背景知识: 当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。 只要在程序中“捕捉”到这个消息,再启动之即可。记住,Android框架说:Don't call me, I'll call you back。我们要做的是做好接收这个消息的准备,而实现的手段就是实现一个BroadcastReceiver。
代码解析:
1、界面Activity:SayHello.java
package com.ghstudio.BootStartDemo;    import android.app.Activity;  import android.os.Bundle;  import android.widget.TextView;    public class SayHello extends Activity {        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);              TextView tv = new TextView(this);          tv.setText("Hello. I started!");              setContentView(tv);      }  }  
这段代码很简单,当Activity启动时,创建一个TextView,用它显示"Hello. I started!"字样。
2、接收广播消息:BootBroadcastReceiver.java
package com.ghstudio.BootStartDemo;    import android.content.BroadcastReceiver;  import android.content.Context;  import android.content.Intent;    public class BootBroadcastReceiver extends BroadcastReceiver {     static final String ACTION = "android.intent.action.BOOT_COMPLETED";      @Override   public void onReceive(Context context, Intent intent) {        if (intent.getAction().equals(ACTION)){     Intent sayHelloIntent=new Intent(context,SayHello.class);     sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       context.startActivity(sayHelloIntent);    }   }    }  
该类派生自BroadcastReceiver,覆载方法onReceive中,检测接收到的Intent是否符合BOOT_COMPLETED,如果符合,则启动SayHello那个Activity。
3、配置文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.ghstudio.BootStartDemo"        android:versionCode="1"        android:versionName="1.0">      <application android:icon="@drawable/icon" android:label="@string/app_name">          <activity android:name=".SayHello"                    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>      <uses-sdk android:minSdkVersion="3" />       <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>    </manifest>   
注意其中粗体字那一部分,该节点向系统注册了一个receiver,子节点intent-filter表示接收 android.intent.action.BOOT_COMPLETED消息。不要忘记配置 android.permission.RECEIVE_BOOT_COMPLETED权限。


===========================================

1.要做的就是监听系统发出的broadcast

public class FlorenceText extends BroadcastReceiver{        public void onReceive(Context context, Intent intent) {                if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){                        start(context);                }       }}

2.在AndroidMenifest.xml里还要将receiver加上, 并写明程序的入口就是FlorenceTest

    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <receiver android:name=".main.FlorenceTest"                android:exported="true"                android:process=":remote">            <intent-filter >                        <action android:name="android.intent.action.BOOT_COMPLETED" />                        <category android:name="android.intent.category.HOME"/>            </intent-filter>        </receiver>  </application>

这样,程序就可以在开机时自动运行了

转自:http://blog.csdn.net/wh_19910525/article/details/7921685

更多相关文章

  1. Android应用程序安装过程浅析
  2. Android应用程序键盘(Keyboard)消息处理机制分析(27)
  3. Android应用程序键盘(Keyboard)消息处理机制分析(17)
  4. android 根据文件的扩展名选择用什么应用程序打开
  5. Android应用程序 启动画面
  6. Android Activity管理类,用于Activity管理和应用程序退出
  7. android中得到所有安装的应用程序及区分其是否为系统应用程序还
  8. android 调用mail程序发送邮件

随机推荐

  1. Vivado一项全新项目功能——可配置的报告
  2. Python高能小技巧:了解bytes与str的区别
  3. NSGA-II多目标优化算法讲解(附MATLAB代码)
  4. CW节约算法构造VRPTW初始解(附MATLAB代码)
  5. 数字化转型最致命的5个误区
  6. [DM]分类-贝叶斯分类
  7. [DM]分类-决策树
  8. [DM]分类-神经网络
  9. flex容器中的四个属性的功能演示
  10. 323作业