Android开机自启动
Android开机自启动
要想在Android系统中实现开机启动,很简单,只需要几个步骤就可以了。
1.定义广播类
2.Manifest.xml中注册广播类
3.添加权限
下面就是具体操作了。
首先,我们来定义广播类。
创建一个类BootReceiver,使其继承BroadcastReceiver。
重写一些必要的Java函数
- package cn.etzmico;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class BootReceiver extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
- Log.d("BootReceiver", "system boot completed");
- // context, AutoRun.class
- Intent newnewIntent = new Intent(context, AutoRun.class);
- /* MyActivity action defined in AndroidManifest.xml */
- newIntent.setAction("android.intent.action.MAIN");
- /* MyActivity category defined in AndroidManifest.xml */
- newIntent.addCategory("android.intent.category.LAUNCHER");
- /*
- * If activity is not launched in Activity environment, this flag is
- * mandatory to set
- */
- newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- /* if you want to start a service, follow below method */
- context.startActivity(newIntent);
- }
- }
- }
AutoRun.class就是程序运行的Activity。
其次,在Manifest.xml中注册广播类
- <receiver android:name=".BootReceiver" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </receiver>
最后,再添加上权限就可以了
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
这样,我们就实现了Android系统的开机自启动,切勿忘记Manifest.xml中的操作!
本文Android开机自启动 工程资源下载:
免费下载地址在 http://linux.bkjia.com/
用户名与密码都是www.bkjia.com
具体下载目录在 /pub/Android源码集锦/2011年/10月/Android开机自启动/
评论暂时关闭