开机启动Service

来源:互联网 发布:郝蕾辱骂河南人 知乎 编辑:程序博客网 时间:2024/06/08 07:49

 

BootReceiver.java

 

public class BootReceiver extends BroadcastReceiver {
    public void onReceive(Context ctx, Intent intent) {
        Log.d("BootReceiver", "system boot completed");

        // start service
        Intent s = new Intent(ctx, ServiceName.class);
        ctx.startService(s);
    }

 

}

 


AndroidManifest.xml

 

        <service android:name=".ServiceName">
            <intent-filter>
                <action android:name="android.intent.action._SERVICE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

 

        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.actegory.HOME" />
            </intent-filter>
        </receiver>