避免在收件箱..android中接收短信

时间:2014-08-12 09:55:01

标签: android sms smsmanager

我想向短信中心发送短信并从中心接收结果短信,并在Edittext显示结果...我的主要活动是LoginActivity ..当我运行此程序时,点击按钮后,消息成功发送到短信中心。但是中心的结果短信没有显示在我的程序中,但显示在我的收件箱中!我想避免在收件箱中收到mesesage,我想在我的应用程序中收到此消息!

LoginActivity中的我的代码是:

 private void notifyIncoming(String message)
    {
        messageRes.setText(message);
    }

    private BroadcastReceiver receiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {

            Object[] pdus = (Object[])intent.getExtras().get("pdus");
            StringBuilder message = new StringBuilder();
            SmsMessage messageSegment;

            for (int i=0;i < pdus.length;i++)
            {
                messageSegment = SmsMessage.createFromPdu((byte[]) pdus[i]);
                message.append(messageSegment.getDisplayMessageBody());
            }

           notifyIncoming(message.toString());

        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");


        setContentView(R.layout.activity_login);
        // Set up the login form.
        messageRes = (EditText) findViewById(R.id.code3_txt);

        findViewById(R.id.sign_in_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        @SuppressWarnings("deprecation")
                        String phoneNumber = "0123456789";          
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(phoneNumber, null, "Hi", null, null);
                        registerReceiver(receiver, mIntentFilter);

                    }
                });
    }

我的Manifest.XML是:

<manifest.................. >


    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>


    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        ............ >

     <activity
            android:name="com.example.accelometer.LoginActivity"
            ............... >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.example.accelometer.LoginActivity" >
            <intent-filter android:priority="100">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

更新

更改onCreate Func。

 @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                mIntentFilter = new IntentFilter();
                mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
                registerReceiver(receiver, mIntentFilter);


                setContentView(R.layout.activity_login);
                // Set up the login form.
                messageRes = (EditText) findViewById(R.id.code3_txt);

                findViewById(R.id.sign_in_button).setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                @SuppressWarnings("deprecation")
                                String phoneNumber = "0123456789";          
                                SmsManager smsManager = SmsManager.getDefault();
                                smsManager.sendTextMessage(phoneNumber, null, "Hi", null, null);    
                            }
                        });
            }

更新2

我从<receiver>删除了manifest块...我可以在我的应用中收到短信,但我想避免在收件箱或其他短信应用中显示此短信,例如视频群聊和其他

1 个答案:

答案 0 :(得分:2)

对于您要做的事情,KitKat之前有许多解决方案。这可能是最好的例子:

Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4)

使用KitKat +,我在这里发布了一个需要应用程序的解决方案:

How to prevent SMS going to inbox in Android Kitkat

相关问题