user2855778
user2855778

Reputation: 137

Avoid Receive SMS in inbox ..android

I want to send a SMS to a SMS center and receive a result sms from it and show that result in an Edittext... my main activity is LoginActivity.. When I run this program, after click on button, The message sent to SMS center successfully. But result SMS of center didn't show in my program , But shown in my Inbox! I want to to avoid recieve mesesage in Inbox and I want to recieve this message in my app!

My codes in LoginActivity is:

 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);

                    }
                });
    }

and my Manifest.XML is:

<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>

Update:

change on 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);    
                            }
                        });
            }

Update 2

I delete <receiver> block from manifest..and I can receive SMS in my app, But I want to avoid show this SMS in inbox or other SMS apps like hangout and other

Upvotes: 1

Views: 1052

Answers (1)

Jim
Jim

Reputation: 10278

For what you are trying to do, there are many solutions pre-KitKat. This is probably the best example:

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

With KitKat+, I posted here about one solution that requires an app:

How to prevent SMS going to inbox in Android Kitkat

Upvotes: 2

Related Questions