简体   繁体   中英

android BroadcastReceiver: onReceive() method of Receiver is not getting called

I have registered a receiver in manifest.

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.receiverdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="Receiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.example.receiverdemo.RECEIVER" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

and I am sending broadcast from adb shell as

adb shell am broadcast -a com.example.receiverdemo.RECEIVER

the above command should get executed properly and the onReceive() method of Receiver should get called after that.

This is the expected behavior.

I can see this behavior on emulator and galaxy nexus both running android 4.2 but this is not working as expected on my HTC one X and HTC-desire-x both running android 4.1.1, the onReceive() method is not getting called, even though am broadcast command is getting executed properly.

I can see the output as

shell@android:/ $ am broadcast -a com.example.receiverdemo.RECEIVER
am broadcast -a com.example.receiverdemo.RECEIVER
Broadcasting: Intent { act=com.example.receiverdemo.RECEIVER }
Broadcast completed: result=0

Receiver class is as follows,

public class Receiver extends BroadcastReceiver {

    public static final String ACTION_RECEIVER = "com.example.receiverdemo.RECEIVER";

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.i("DEMO", "Working");
    }
}

I am not getting what is going wrong here. Is it something related with API versions or ROMs or some developer settings or something else?

我将设备植根,并以root特权从adb shell处执行了该命令,并且它按预期运行。

您也可以尝试为此接收者添加android:permission="com.google.android.c2dm.permission.SEND"评论。

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