BroadcastReceiver onReceive is not fired

2019-07-25 17:36发布

问题:

I'm trying to cope with SMS receiving funcions in Android. I read a lot of related topics on Stackoverflow and other sites and I tried with a very simple Class that simply print on the console that a message has been received and the message, but I cannot figure out why it doesn't work.

I see that similar questions remained unanswered in the past (see Android - Broadcast Receiver not being fired)

Hope someone could find where's the problem with my code.

Code:

package com.storassa.android.smsapp;

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

public class SmsReceiver extends BroadcastReceiver {

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Intent recieved: " + intent.getAction());

if (intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
System.out.println("Message recieved: " + messages[0].getMessageBody());
        }
    }
}
     }
}

And Manifest

<manifest package="com.storassa.android.smsapp"
    android:versionCode="1"
    android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="6"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

    <application android:label="@string/app_name" android:permission="android.permission.RECEIVE_SMS">
        <receiver android:name="com.storassa.android.smsapp.SmsReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>

    </application>
</manifest>

回答1:

I tried your code, and it's working fine for me.

The post you are referring to has the problem with the setPriority set to 999 and still the broadcast aren't received.

Try setting the priority to Integer.MAX_VALUE or when in xml it should be 2147483647

Some SMS apps will use this Integer.MAX_VALUE as their priority and hence "eat" all messages received.

If it doesn't help setting setPriority to Integer.MAX_VALUE or 2147483647, then try uninstalling all apps that are handling incoming SMS'es and see if the SMS'es comes through to your app.

If this help, then it's one of the applications already installed that are eating the SMS'es.

A work-around for this, would be always to install your application as the first one. By doing this, you will always be the first application receiving the SMS broadcast.

EDIT: setting the priority to Integer.MAX_VALUE or 2147483647 is a hack and should possibly not be used. The setPriority() method only states priority from -1000 - 1000. However I use it in an app myself, but I only handle specific SMS'es received from specific numbers, so I don't see a problem in it.



回答2:

This is happens when you also have installed other application which has high priority then the current one apps, so increase your priority it will start working, i also got this problem many time, in one app my priority was 100 and it was not working but when i changed it to 999, then it starts working, So try to set different and large no priority and check it again.



回答3:

//In on resume and on pause of actvities register the receiver and also in manifest

    protected void onPause(){

        super.onPause();
    unregisterReceiver(SmsReceiver ); 

    }



        protected void onResume()
{


            super.onResume();

            registerReceiver(SmsReceiver , 
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

        }


    private BroadcastReceiver SmsReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                        }


        };