gpt4 book ai didi

android - Broadcast receiver 不能在 Android4.0 模拟器上工作

转载 作者:行者123 更新时间:2023-11-29 16:17:50 26 4
gpt4 key购买 nike

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

<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 中广播接收器发生了什么变化。非常感谢。

最佳答案

安装您的应用程序后,用户需要手动启动您的 Activity,然后您的任何 BroadcastReceivers 才会生效,从 Android 3.1 开始。

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

关于android - Broadcast receiver 不能在 Android4.0 模拟器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8471930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com