Start intent after Reboot

∥☆過路亽.° 提交于 2019-12-06 16:53:50

In your manifest:

<service android:exported="false" android:name=".service.YourService" android:enabled="true"></service>
        <receiver android:name=".service.YourBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver> 

Also add permission in manifest:

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

YourBootReceiver:

public class YourBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Intent serviceIntent = new Intent(arg0, YourService.class);
        arg0.startService(serviceIntent);
    }

You have to add RECEIVE_BOOT_COMPLETED permission in your manifest file to get notified :

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