package com.example.startest;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
//后边的XXX.class就是要启动的服务
activityAutoService(arg0,MyService.class);
//activityAutoStart(arg0,MainActivity.class);
Log.i("test", "开机自动服务自动启动.....");
//启动应用,参数为需要自动启动的应用的包名
}
private void activityAutoStart(Context arg0,Class<?> cls){
Intent intent = new Intent(arg0,cls);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
arg0.startActivity(intent);
}
private void activityAutoService(Context arg0,Class<?> cls){
Intent service = new Intent(arg0,cls);
arg0.startService(service);
}
}
<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>