背景知识:当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

转:android中APK开机自动运行的更多相关文章

  1. [转载] 在Linux中,开机自动运行普通用户的脚本程序

    FROM:http://blog.csdn.net/sinboy/article/details/2466225 FROM:http://www.2cto.com/os/201006/50680.ht ...

  2. Android---让你的APK程序开机自动运行(转)

    转自: http://blog.sina.com.cn/s/blog_72f6e45701014l6t.html 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service. ...

  3. 【转】]Android实现开机自动运行程序

    有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call y ...

  4. Android实现开机自动运行程序

    有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call y ...

  5. Android开机自动运行APP——BroadcastReceiver

    前言: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll ca ...

  6. C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)

    #region//开机自动运行        private void CB_Auto_CheckedChanged(object sender, EventArgs e)        {//CB_ ...

  7. C# 设置程序开机自动运行(+注册表项)

    有时候我们需要让软件安装好了,开机自动运行,这时我们需要把启动项加载到注册表中,需要注意的时现在很多杀毒软件在其他软件更改注册表的时候会有提示,可能会阻止.下面代码包含增加启动项到注册表和删除启动项. ...

  8. C#安装程序制作让安装后的程序开机自动运行

    1.创建安装项目后要在自己的解决方案是添加一个新的类库项目(ClassLibrary1),并在新类库中添加一下安装程序类(Installer1),在Installer1类中添加如下代码: string ...

  9. /etc/rc.local 与 /etc/init.d Linux 开机自动运行程序

    1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里 --------引用---------------------- 在完成 run level 3 ...

随机推荐

  1. git 初级

    以前工作中用到git,但没有总结,这次借鉴其它博客加上自己实践,总结git的简单用法 首先安装.... 打开一个文件右击git bash 弹出来一个jit界面 git config http.post ...

  2. windows 下安装elasticsearch

    1.下载 elasticsearch-2.3.3.zip 注意::elasticsearch 版本太高的话,java运行不起来 elasticsearch-1.5.2下载地址 http://downl ...

  3. 在COM接口中不要使用同时出现只是大小写不同的名字作为属性名、函数名或者参数名

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在COM接口中不要使用同时出现只是大小写不同的名字作为属性名.函数名或者参数名.

  4. spring的有状态BEAN和无状态BEAN

    有状态会话bean   :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”:一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一 ...

  5. os4开始CLLocationManager类中不支持locationServicesEnabled属性了

    locationServicesEnabled改为类方法 //判断定位服务是否开启 if ([CLLocationManager locationServicesEnabled]) { }

  6. 多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)

    Hello everyone! For a week I've been looking on how to make a keyboard work! I managed to figure it ...

  7. MongoDB的地埋空间数据存储、空间索引以及空间查询

    一.关于MongoDB 在众多NoSQL数据库,MongoDB是一个优秀的产品.其官方介绍如下: MongoDB (from "humongous") is a scalable, ...

  8. MVC架构杂谈

    来源:伯乐在线专栏作者 - 林欣达 链接:http://ios.jobbole.com/86895/ 点击 → 了解如何加入专栏作者 前言 MVC是软件工程中的一种软件架构模式,它把软件系统分为三个基 ...

  9. Socket解决粘包问题2

    在AsynServer中对接收函数增加接收判断,如果收到客户端发送的请求信息,则发送10个测试包给发送端,否则继续接收,修改后的接收代码如下: private void AsynReceive() { ...

  10. html笔记04:在html之中导入css两种常见方法

    1.导入式: <html> <head> <title></title> <style type="text/css"> ...