广播接收器不工作 [英] BroadcastReceiver is not working
问题描述
我已经实现这个广播reciever:
I have implemented this broadcast reciever:
public class ServiceManager extends BroadcastReceiver {
private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
private final String BOOT_ACTION_FIRST_LAUNCH = "android.intent.action.PACKAGE_FIRST_LAUNCH";
private final String BOOT_ACTION_RESTARTED = "android.intent.action.PACKAGE_RESTARTED";
@Override
public void onReceive(Context context, Intent intent) {
// All registered broadcasts are received by this
String action = intent.getAction();
if (action.equalsIgnoreCase(BOOT_ACTION) || action.equalsIgnoreCase(BOOT_ACTION_FIRST_LAUNCH) ||
action.equalsIgnoreCase(BOOT_ACTION_RESTARTED)) {
// TODO: Action
}
}
}
AndroidManifest.xml中
AndroidManifest.xml
<receiver android:name="package.service.ServiceManager" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
</intent-filter>
</receiver>
该BOOT_COMPLETED行动工作的权利,但是,在PACKAGE_FIRST_LAUNCH和PACKAGE_RESTARTED不工作。我需要我的时候启动我的应用程序来启动我的广播接收器,这就是为什么我使用这些操作。但是,当我启动或重新启动应用程序,接收器不工作。它只有当我重新启动我的手机作品。是否有一些错误我的源?
The BOOT_COMPLETED action is working right, but, the PACKAGE_FIRST_LAUNCH and PACKAGE_RESTARTED are not working. I need to launch my broadcast receiver when I launch my app, that's why I'm using these actions. But, when I launch or restart the app, the receiver is not working. It only works when I restart my mobile phone. Are there something wrong in my source?
推荐答案
从逻辑上看来,一旦你的应用系统启动后运行的第一次 PACKAGE_FIRST_LAUNCH
将播出/重新启动。和 PACKAGE_RESTARTED
如果您的应用程序栈的活动被删除,应播放,然后您的应用程序被点击重新开始(如重启)。
Logically it seems that PACKAGE_FIRST_LAUNCH
will be broadcasted once your app is run for the first time after boot/reboot. And PACKAGE_RESTARTED
should be broadcasted if your application activity stack is removed and then your app is clicked to start again (like restart).
不过,你可以简单地通过当过你的应用程序启动(可能是从你的第一个活动)广播的自定义操作字符串实现这一点。
However, you may simply achieve this by broadcasting a custom action string when ever your app is launched (perhaps from your first activity).
这篇关于广播接收器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!