赞
踩
功能:开机后,应用自启,本例在监听到开机事件后,直接启动主界面,如非流氓软件,不建议这样操作,影响体验^_^。
1、添加权限,静态注册广播(在程序未启动的情况下就能够就收到广播)
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name="receiver.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
2、新建广播接收者,继承BroadcastReceiver,复写onReceive()方法执行自定义操作,Toast提示文字并打开应用。
package receiver;
import activity.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* 接收开机广播
* @author chenql
*/
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("BootCompletedReceiver", "onReceive");
Toast.makeText(context, "Boot completed.", Toast.LENGTH_SHORT).show();
MainActivity.actionStart(context);
}
}
3、MainActivity启动方法
public static void actionStart(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。