简体   繁体   中英

invoke broadcast receiver on receiving data sms in android

i am sending data sms to an emulator through another emulator,My data sms sent there but my BroadcastReceiver doesn't get invoked. I tried sending/receiving a text SMS and that works absolutely fine but I need to specify a port so only my application can listen for the SMS.

This question has been asked twice but never answered: kindly help me if you know the solution

Receiver.java:

public class Receiver extends BroadcastReceiver{

// Declaration of Variable for getting the message in it    


/*
*Called when The Message Received
*/
    @Override
    public void onReceive(Context context, Intent intent) 
    { 
        try{


        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";   
        byte[] data = null;

        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 frommm " + msgs[i].getOriginatingAddress();                     
                str += " :";

                data=msgs[i].getUserData();

                for(int index=0; index<data.length; ++index)
                {
                       str += Character.toString((char)data[index]);


                }




            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            System.out.println("The message is "+ str);


        }    
    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    }

Menifest file code: (only relevant code)

< receiver android:name=".Receiver" >

   <  intent-filter  > 

      <  action android:name="android.intent.action.DATA_SMS_RECEIVED" /  >

      <  data android:scheme="sms" /  >

      <  data android:host="localhost"/  >

      <  data android:port="5009"/  >

      <  action android:name="android.intent.action.BOOT_COMPLETED" /  >

            <category android:name="android.intent.category.HOME" /  >  

        <  /intent-filter  > 

    <  /receiver  >

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

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

<  uses-permission android:name="android.permission.INTERNET" /     >

<  uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/  >

Data message sending code:

message contains String that i want to send

byte []b=message.getBytes();
Short sObj2 = 5009;

SmsManager sms = SmsManager.getDefault();
sms.sendDataMessage(phoneNumber,null,sObj2,b,sentPI, deliveredPI)

Did you register your receiver? You need to register your BroadcastReceiver. Create an IntentFilter and pass it to registerReceiver.

Have you confirmed that the native SMS app is receiving the SMS in the emulator? If this is the case, that means your SMS is actually being received. I have never sent an SMS between emulators and have only done this with real devices. The problem I had was that, prior to registering the receiver, I needed to set the priority for it to be higher than any other app that could possibly respond, including the native SMS app. You can do it like this:

filterSMSReceived.setPriority(Integer.MAX_VALUE);

This will make it get notification before any other app can.

Looking at http://code.google.com/p/android/issues/detail?id=1576 it does not appear that data SMS works on an emulator. Did you try the comment #23 from http://code.google.com/p/android/issues/detail?id=1576 on a real device?

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