start my service on phone restart in android

∥☆過路亽.° 提交于 2019-12-04 21:40:12

Make Sure u have the permission in AndroidManifest File :

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

Change this to :

@Override
public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent(context,BackgroundService.class);
    startService(serviceIntent);
    }
Basbous

RECEIVE_BOOT_COMPLETED: Broadcast Action: This is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization, such as installing alarms. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.

Refrence

Stackoverflow Question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!