android中开机自动运行程序(转) 原文地址: http://yueguc.javaeye.com/blog/620410
背景知识:当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权限。

延伸思考:在多数情况下,要自动运行的不是有界面的程序,而是在后台运行的service。此时,就要用startService来启动相应的 service了。

代码下载

更多相关文章

  1. Android手机客户端与Servlet交换数据(by quqi99)
  2. Android(安卓)入门到精通 (界面编程#2-Menu)
  3. Android——最新LitePal使用
  4. 通过ijetty获取android设备的截图
  5. Android清除本地数据缓存代码案例
  6. android闹钟――原代码
  7. 搭建Android(安卓)NDK开发环境
  8. BroadcastReciver 广播接收者---DAY06 2014 06 03
  9. 【Android】ToolBar设置NavigationIcon不显示异常或自定义失败异

随机推荐

  1. RotateAnimation 设置旋转中心点、不停顿
  2. Android:为控件绑定监听器
  3. 申请Android Maps API Key-及出现的错误
  4. 什么是aidl?Android(安卓)AIDL详解
  5. Android Ams浅析
  6. Android 实现直接拒接来电
  7. Android:控件布局(相对布局)RelativeLayout
  8. android ICS4.0.3 改变默认字体大小
  9. android带动画的圆形进度条
  10. android 设置界面