简体   繁体   中英

Receiving SMS messages in my app

I am creating an app that receives SMS text messages, I have created a class that extends BroadcastReceiver to receive and display SMS messagesin a toast within the app. I have also added permissions too my manifest along with the declaration of the receiver. The problem is it still does not seem to display the message. It will only appear on the top bar as a notification. Hopefully someone will be able to see where i am going wrong. Do i have to have some code in my mainactivity in order to display the message?? or should it display in my all my activities straight from the receiver clas?? code below:

UPDATE
I ended up deleting my receiver and recreating everything, now it seems to work fine
SmsReceiver.java:

 public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();


    if (extras ==null)
        return;
    //toast
    Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();

    Object[] pdus = (Object[]) extras.get("pdus");

    for (int i = 0; i < pdus.length; i++) {
        SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
        String sender = SMessage.getOriginatingAddress();
        String body = SMessage.getMessageBody().toString();

        Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body);

        context.sendBroadcast(in);
        this.abortBroadcast();

    }
}
}

The problem is it still does not seem to display the message. It will only appear on the top bar as a notification

do you know what is - Ordered Broadcast ?

in case you don't know - it's a broadcast that receives synchronously (one after another) by all receivers that registered for this broadcast. you can set in the manifest receiver deceleration a priority , and each one of the receivers can control if the broadcast will pass on in the priorities chain to additional registered receivers by using the aboartBroadcast() method.

what's to this and receiving SMS? - the system sends ordered broadcast when SMS receives. if your receivers priority is lower then the "default SMS app" priority - then what usually happens is that the application that got the broadcast first will cancel the broadcast, and you'll never receives it.

to get the broadcast before the default SMS application you have to declare your receiver with high priority (providing priority = 1000 should do the trick).

without providing priority - it's automatically set to zero.

you can have a look on this example - How Can i INTERCEPT an Incoming SMS With A specific Text

also make sure you declared the permission to RECEIVE_SMS in the manifest

Try starting again and setting up your receiver like so....

 public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();


    if (extras ==null)
        return;
    //toast
    Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();

    Object[] pdus = (Object[]) extras.get("pdus");

    for (int i = 0; i < pdus.length; i++) {
        SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
        String sender = SMessage.getOriginatingAddress();
        String body = SMessage.getMessageBody().toString();

        Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_msg", sender+":"+body);

        context.sendBroadcast(in);
        this.abortBroadcast();

    }
}
}
@Override
public void onReceive(Context context, Intent arg1) {
        SmsMessage msgs[] = getMessagesFromIntent(arg1);
        for (int i = 0; i < msgs.length; i++) {
               SmsMessage mesg = msgs[i];
               Toast toast = Toast.makeText(context, mesg.getDisplayMessageBody(), Toast.LENGTH_LONG);
               toast.show();
        }
}
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
  Bundle extras = intent.getExtras();
  SmsMessage[] msgs = null;
  String str = "";
  // retrieve the SMS message received ::
  Object[] pdus = (Object[]) extras.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";
   // Toast to display SMS ::
   Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

   // Send SMS ::
   SmsManager sms = SmsManager.getDefault();
   sms.sendTextMessage("put your phoneNumber(emulatorPort)", null, str, null, null);

    }
}
}

-- Have nice time ! Don't forget to add permissions !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM