广播接收器无法在Android4.0仿真器上运行

时间:2011-12-12 09:02:05

标签: android

我创建了一个只包含一个接收器的项目。有些代码如下:

<receiver android:name=".InputReceive" android.enabled = "true">
    <intent-filter>
    <action android:name="com.myproject.BackButton" />
</intent-filter>
</receiver>

public class InputReceive extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    String action = arg1.getAction();
    if(action.equals("com.myproject.BackButton")){
        back();
    }
}

private void back(){
    Log.e("wwq", "back");
}

}

当我在cmd中运行命令时

adb shell am broadcast -a com.myproject.BackButton

logcat中没有“back”等日志。接收者没有回应。 记录如下:

12-12 16:33:05.431: W/ActivityManager(98): Timeout of broadcast BroadcastRecord{415fcd58 com.android.server.action.NETWORK_STATS_POLL} - receiver=android.app.LoadedApk$ReceiverDispatcher$InnerReceiver@4135ff18, started 10051ms ago
12-12 16:33:05.431: W/ActivityManager(98): Receiver during timeout: BroadcastFilter{41360160 ReceiverList{413600e8 98 system/1000 local:4135ff18}}
12-12 16:33:15.609: W/ActivityManager(98): finishReceiver called but no pending broadcasts

此问题仅发生在Android 4.0模拟器上。谁知道Android 4.0中的广播接收器发生了什么变化。非常感谢。

1 个答案:

答案 0 :(得分:4)

安装完应用程序后,用户需要在任何BroadcastReceivers生效之前手动启动您的活动,自Android 3.1起。

另请注意,它是android:enabled,而不是android.enabled