Android BroadcastReceiver, auto run service after reboot of device

送分小仙女□ 提交于 2019-11-26 16:04:11

First do this

1) In your <manifest> element:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

EDIT :

Simple and smart solution:

--> Autostart Service on Device Boot

You forgot about the permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Though all the above answers were correct, however from Android Oreo they put some limitation on Background Services.

Check Background Execution Limits to know more about background limits in Android O.

You can't start a Service directly from BroadCastReceiver when the application is in the background.

However, you can start a foreground service from the receiver by calling startForegroundService() or use JobIntentService as there is no such limitation with JobIntentService.

saksham agarwal

also try these permissions,it may help you .ifyou are using htc phones then these permissions are required

<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>

Use this code on your Broadcast Receiver class:

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
   Intent service = new Intent(context, MsgPushService.class);  
    context.startService(service);  
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!