Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

android - Broadcast Receiver not working for SMS

First of all I already searched for possible solutions, tried everything and it still didn't work. I must be missing something.

I am trying to create an app that receives/reads and writes SMS. the write part is working just fine, my broadcast receiver just doesn't catch broadcast.

AndroidManifest.xml

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" /> 
    <uses-permission android:name="android.permission.READ_SMS" /> 
...
        <receiver android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
        </receiver>

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "ON RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
    Log.d("ON ","RECEIVE");
     Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        // Create messages for each incoming PDU
        for (int n = 0; n < messages.length; n++) {
            sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
        }
        for (SmsMessage msg : sms) {
            Log.e("RECEIVED MSG",":"+msg.getMessageBody());
            // Verify if the message came from our known sender

        }
}

none of the Logs or toasts are fired.

Tried changing action on manifest to android.intent.action.AIRPLANE_MODE just to test the declaration and the broadcast was received, is just not working for the SMS.

UPDATED

Tried on a different phone and it worked. Must be because I am using Handcent SMS, and some how its blocking the broadcast. Either way I need it to be working on every phone independent of the applications installed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found a topic that answers my doubt: Suppress / Block BroadcastReceiver in another app.

Even with the priority set to the maximum possible (999), if another app has the same priority, in this case the Handcent SMS app, the first application that will receive the broadcast is the one that was first installed by the user.

In my case was the Handcent SMS and because it aborts the broadcast when receiving it, my app doesn't receive anything.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...