[英]Android After Reboot Broadcast Receiver is not running
I used this permission:我使用了这个权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and the receiver is:接收者是:
<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and receiver in code is:代码中的接收器是:
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");
if (intent != null) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
System.out.println("Called on REBOOT");
// start a new service and repeat using alarm manager
break;
default:
break;
}
}
}
After reboot it's still not getting called in lollipop, but on marshmallow it's running.重新启动后,它仍然没有被棒棒糖调用,但在棉花糖上它正在运行。
try to put this line in your receiver's intent-filter.尝试将此行放在接收器的意图过滤器中。
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
If your application is installed on the SD card, you should register this to get the android.intent.action.BOOT_COMPLETED event.如果您的应用程序安装在 SD 卡上,您应该注册它以获取 android.intent.action.BOOT_COMPLETED 事件。
Updated: Since your app is using alarm service, it should not be installed on external storage.更新:由于您的应用程序正在使用警报服务,因此不应将其安装在外部存储上。 Reference: http://developer.android.com/guide/topics/data/install-location.html
参考: http : //developer.android.com/guide/topics/data/install-location.html
Whenever the platform boot is completed, an intent with android.intent.action.BOOT_COMPLETED action is broadcasted.每当平台启动完成时,就会广播一个带有 android.intent.action.BOOT_COMPLETED 动作的意图。 You need to register your application to receive this intent.
您需要注册您的应用程序才能接收此意图。 For registering add this to your AndroidManifest.xml
对于注册,将此添加到您的 AndroidManifest.xml
<receiver android:name=".ServiceManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
So you will have ServiceManager as broadcast receiver to receive the intent for boot event.因此,您将使用 ServiceManager 作为广播接收器来接收启动事件的意图。 The ServiceManager class shall be as follows:
ServiceManager 类应如下所示:
public class ServiceManager extends BroadcastReceiver {
Context mContext;
private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// All registered broadcasts are received by this
mContext = context;
String action = intent.getAction();
if (action.equalsIgnoreCase(BOOT_ACTION)) {
//check for boot complete event & start your service
startService();
}
}
private void startService() {
//here, you will start your service
Intent mServiceIntent = new Intent();
mServiceIntent.setAction("com.bootservice.test.DataService");
mContext.startService(mServiceIntent);
}
}
Since we are starting the Service, it too must be mentioned in AndroidManifest:由于我们正在启动服务,因此在 AndroidManifest 中也必须提及它:
<service android:name=".LocationService">
<intent-filter>
<action android:name="com.bootservice.test.DataService"/>
</intent-filter>
</service>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.