简体   繁体   中英

android: getting current message received

How do I get the current message that is been received in the method:

public class RecieveSMS extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(SMS_RECEIVED)) {
        try {

How would I retrieve the number, time and body? As I am trying to save the messages then abort the broadcast I've tried querying the content://sms but if they isn't already a message from that user it seams to receive the first message then block the rest.

public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String messageContent = "";
    String number = "";

    if (bundle != null)
    {
        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]);                
            number = msgs[i].getOriginatingAddress();
            messageContent = msgs[i].getMessageBody().toString();
        }
     }                         

}

Check this out: examples for creating broadcast reciever for SMS:

sms-messaging-in-android-send-and-receive

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