package com.example.test1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
public class BootBroadcastReceiver extends BroadcastReceiver{
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
mContext = context;
sendMessage(11);
}
}
private void sendMessage(int str){
Message msg = new Message();
msg.arg1 = str;
MyHandler.sendMessageDelayed(msg, 5000);
}
private Handler MyHandler = new Handler() {
public void handleMessage(Message msg) {
int msgId = msg.arg1;
switch (msgId) {
case 11:
Intent mainActivityIntent = new Intent(mContext, MainActivity.class);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(mainActivityIntent);
Toast.makeText(mContext, "启动APK了!", Toast.LENGTH_LONG).show();
break;
}
};
};
}
package com.example.test1;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>