简体   繁体   中英

BroadcastReceiver onReceive not firing

I am currently trying to create a phone number verification on registering on my Android app. Thank you for your help to read my long post!

Here is my Manifest file with other activities omitted

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.opensem"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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


    <application
        android:name=".AppHelper"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        >
        <receiver android:name=".SMSReceiver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
        <activity
            android:name=".OpenSem"
            android:label="@string/title_activity_opensem" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Here is my class which I want to show a log on receive SMS

public class SMSReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TAG", "Received");
    }
}

Here is my class sending the SMS

public class RegisterPageSubmitListener implements OnClickListener{
    public void onClick(View v) {
        SmsManager sm = SmsManager.getDefault();
        number = ParamList.get("phone");
        code = randInt(100000, 999999) + "";
        sm.sendTextMessage(number, null, code, null, null);
    }
}

My problem is that I successfully send the SMS and received it on my phone, but the "Received" Log was never shown. I came across the priority problem on StackOverflow and thus I added the android:priority="2147483647" in my manifest. I then added these lines (Copied from another SO post) in other activities to see if my receiver is properly registered.

    Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
    for (ResolveInfo info : infos) {
        Log.d("OpenSEM", "Receiver name:" + info.activityInfo.name + ";     priority=" + info.priority);
    }

and the resulting log is shown below:

08-01 15:27:10.417: D/OpenSEM(12880): Receiver name:com.android.mms.transaction.HighPrivilegedSmsReceiver;     priority=1000
08-01 15:27:10.417: D/OpenSEM(12880): Receiver name:com.example.opensem.SMSReceiver;     priority=999
08-01 15:27:10.417: D/OpenSEM(12880): Receiver name:com.android.mms.transaction.PrivilegedSmsReceiver;     priority=0

It seems that my receiver's priority was set to 999 instead of 2147483647. Also, I didn't install any SMS applications, so the other priority=1000 receiver seems to belong to the system messaging application.

Did I do anything wrong? I tested my code on a Red MI (MIUI) and Samsung note II. My SMSReceiver seems to be registered and not receiving the SMS which was successfully sent back to both phones. I would really appreciate any help!!

Potato. For the long post.

这可能是您的ROM的限制,即MIUI,我遇到了我的BroadcastReceiver无法接收广播的问题,但是我的应用在Samsung手机上可以。

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