import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("BootReceiver", "system boot completed");
//start activity
if (intent.getAction().equals(ACTION)){
Intent myi=new Intent(context,MainActivity.class);
myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myi);
}
}
}
然后在AndroidManifest.xml MainActivity中添加
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
还需要把BootReceiver在AndroidManifest.xml 中注册
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
然后就可以实现自启动了啊!