背景知识:当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. Kali无法定位软件包的解决方案

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 以前遇到过,后来好久没碰Linux了,忘记了.之后上网搜了一下....呃,发现解 ...

  2. C# .Net基础知识点解答

    原文地址 1. 什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式? 通俗的讲,.Net是微软开发应用程序的一个平台: CLI是C ...

  3. error while loading shared libraries: libgtk-x11-2.0.so.0 2014-05-12 22:49:34

      http://askubuntu.com/questions/356605/ubuntu-13-10-64-bit-machinarium-error-while-loading-shared-l ...

  4. Installing the PHP/MongoDB extension on Mac OSX 10.8

    Installing PHP/MongoDB extension is a two steps task on OSX: Install the autoconf tool required for ...

  5. linux系统磁盘分区之parted

    对于linux的分区通常可以使用fdisk命令工具和parted工具 对于分区表通常有MBR分区表和GPT分区表 对于磁盘大小小于2T的磁盘,我们可以使用fdisk和parted命令工具进行分区 对于 ...

  6. android109 结构体,联合体,枚举,自定义

    #include <stdio.h> #include <stdlib.h> void study(){ printf("吃饭睡觉打李志\n"); } // ...

  7. C++ ORM ODB 入门(三)

    本节介绍ODB的事务与 异常. 数据库操作经常涉及到操作多个表格,或者表格中的多行数据.因此必须保证整个过程是原子性的.ODB为数据库的事务提供了易于使用的接口. 使用odb::databse的相关方 ...

  8. C# - 系统类 - Array类

    Array类 ns:System Array是一个抽象类 表示数组 提供了创建.查找.删除.排序.修改等应用于数组的操作 此类没有公有的实例构造函数 可以使用静态方法CreateInstance创建A ...

  9. HDU1010(bfs)

    #include <stdio.h>#include <iostream>#include <string.h>#include <stdlib.h>u ...

  10. Android 自定义View修炼-仿360手机卫士波浪球进度的实现

    像360卫士的波浪球进度的效果,一般最常用的方法就是 画线的方式,先绘sin线或贝塞尔曲线,然后从左到右绘制竖线,然后再裁剪圆区域. 今天我这用图片bitmap的方式,大概的方法原理是: (1)首先用 ...