Android开机自启动程序

背景知识:当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字
符串常量表示为 android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之
即可。记住,Android框架说:Don''t call me, I''ll call you back。我们要做的是做好接收这个消息的准备,而
实现的手段就是实现一个BroadcastReceiver。

1、界面Activity,BootStartDemo.java文件


public class BootStartDemo extends Activity {
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> @Override
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void onCreate(Bundle savedInstanceState) {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super .onCreate(savedInstanceState);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // 无title
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> requestWindowFeature(Window.FEATURE_NO_TITLE);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // 全屏
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> WindowManager.LayoutParams.FLAG_FULLSCREEN);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> setContentView(R.layout.main);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new Thread() {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> public void run() {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> try {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> sleep( 10000 );
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } catch (Exception e) {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> e.printStackTrace();
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } finally {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> finish(); // 关闭页面
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> }
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> }
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> }.start();
<wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> }
}


这段代码很简单,当Activity 启动时,会显示TextView,用它显示你想显示的字样,并且这个页面只显示10秒后消失。

2、接收广播消息:BootBroadcastReceiver.java


public class BootBroadcastReceiver extends BroadcastReceiver {
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> static final String action_boot= "android.intent.action.BOOT_COMPLETED" ;
<wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> @Override
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void onReceive(Context context, Intent intent) {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (intent.getAction().equals(action_boot)){
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Intent ootStartIntent= new Intent(context,BootStartDemo. class );
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> context.startActivity(ootStartIntent);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> }
<wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> }
<wbr></wbr>
}



该类继续自 BroadcastReceiver,覆载方法 onReceive 中,检测接收到的 Intent 是否符合
BOOT_COMPLETED,如果符合,则启动BootStartDemo这个Activity。

3、配置文件

(1)AndroidManifest.xml :


<?xml version= "1.0" encoding= "utf-8" ?>
<!-- 这是一个开机自启动程序 -->
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr> package = "com.ajie.bootstartdemo"
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr> android:versionCode= "1"
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr> android:versionName= "1.0" >
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> <application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <activity android:name= ".BootStartDemo"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> android:label= "@string/app_name" >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <intent-filter>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <action android:name= "android.intent.action.MAIN" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <category android:name= "android.intent.category.LAUNCHER" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </intent-filter>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </activity>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> <span style= "color: #ff00ff;" ><receiver android:name= ".BootBroadcastReceiver" >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <intent-filter>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <action android:name= "android.intent.action.BOOT_COMPLETED" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <category android:name= "android.intent.category.HOME" />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </intent-filter>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> </receiver>
</span><wbr><wbr><wbr> &lt;/application&gt;</wbr></wbr></wbr>
<span style= "color: #ff00ff;" ><strong><uses-permission android:name= "android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission></strong>
</span></manifest>


注意其中颜色标红那一部分,该节点向系统注册了一个 receiver,子节点 intent-filter 表示接收
android.intent.action.BOOT_COMPLETED 消息。并且还要配置android.permission.RECEIVE_BOOT_COMPLETED权限。

(2)Layout文件,main.xml


<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:orientation= "vertical"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:layout_width= "fill_parent"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:layout_height= "fill_parent"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr><wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> >
<TextView<wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:layout_width= "fill_parent"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:layout_height= "fill_parent"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:text= "@string/boottext"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:textColor= "#5F2DD2"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:background= "#FFFFFF"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:textSize= "60px"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> android:gravity= "center_horizontal"
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> />
</LinearLayout>

完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机,就会显示BootStartDemo这个Activity显示出来的页面。



你可能感兴趣的:(android)