0

I am developing an android application that would let people send and receive SMS to a unique number via my application. I can send SMS but it is appearing in INBOX message box!

I want it to appear in my application

I googled and find this but I do not want it to appear in Toast message, I want it like in What's app in android and how to save all the SMS from this number?

this is the code:

package net.learn2develop.SMSMessaging;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage; 
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{
 @Override
    public void onReceive(Context context, Intent intent) 
   {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }                         
 } 
 }  
andrewsi
  • 10,807
  • 132
  • 35
  • 51
user1257040
  • 119
  • 1
  • 2
  • 12

2 Answers2

0

In your manifest, you should have something that looks like this:

<receiver android:name=".SmsReceiver">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

Note android:priority="999".

In your onReceive() method, the first line should be abortBroadcast();. Timing does matter and you will get bundle from the intent.

Budimir Grom
  • 756
  • 6
  • 12
0

As per: Can we delete an SMS in Android before it reaches the inbox?

Incoming SMS message broadcasts (android.provider.Telephony.SMS_RECEIVED) are delivered as an "ordered broadcast" — meaning that you can tell the system which components should receive the broadcast first.

If you define an android:priority attribute on your SMS-listening , you will then receive the notification before the native SMS application.

At this point, you can cancel the broadcast, preventing it from being propagated to other apps.

Community
  • 1
  • 1
Soham
  • 4,940
  • 3
  • 31
  • 48