配置清单AndroidManifest.xml配置权限和广播
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
监听开机启动的广播处理
package com.example.mytestproj;
import com.example.mytestproj.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
//重写onReceive方法
@Override
public void onReceive(Context context, Intent intent) {
//后边的XXX.class就是要启动的服务
Intent service = new Intent(context,MainActivity.class);
context.startActivity(service);
Log.i("testlog", "开机自动服务自动启动.....");
//启动应用,参数为需要自动启动的应用的包名
}
}