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

简介: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call you back!”总结Android框架,真是说到点子上了。

有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call you back!”总结Android框架,真是说到点子上了。理解这句话的含义,许多有关Android平台上实现某种功能的问题,都能迎刃而解。

 

使用场景:手机开机后,自动运行程序,在屏幕上显示"Hello. I started!"字样。

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

完成后,编译出apk包,安装到模拟器或手机中。关机,重新开机。

 

运行截图:

 

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

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

原文链接:http://blog.csdn.net/grhunter/article/details/5105784 

 

 

目录
相关文章
|
26天前
|
移动开发 监控 安全
mPaaS常见问题之Android集成dexPatch热修复运行时候无法正常进行热更新如何解决
mPaaS(移动平台即服务,Mobile Platform as a Service)是阿里巴巴集团提供的一套移动开发解决方案,它包含了一系列移动开发、测试、监控和运营的工具和服务。以下是mPaaS常见问题的汇总,旨在帮助开发者和企业用户解决在使用mPaaS产品过程中遇到的各种挑战
34 0
|
3月前
|
Linux Android开发
Android 正常运行所需的一系列 Linux 内核接口
Android 正常运行所需的一系列 Linux 内核接口
53 0
|
4月前
|
Java 关系型数据库 数据库
Android App连接真机步骤与APP的开发语言和工程结构讲解以及运行实例(超详细必看)
Android App连接真机步骤与APP的开发语言和工程结构讲解以及运行实例(超详细必看)
36 0
|
7月前
|
Java 数据库 Android开发
性能提示-流畅运行的Android应用
性能提示-流畅运行的Android应用
45 0
|
6月前
|
存储 传感器 定位技术
《移动互联网技术》 第四章 移动应用开发: Android Studio开发环境的使用方法:建立工程,编写源程序,编译链接,安装模拟器,通过模拟器运行和调试程序
《移动互联网技术》 第四章 移动应用开发: Android Studio开发环境的使用方法:建立工程,编写源程序,编译链接,安装模拟器,通过模拟器运行和调试程序
63 0
|
6月前
|
Java Android开发 开发者
1024程序节|Android框架之一 BRVAH【BaseRecyclerViewAdapterHelper】使用demo
BRVAH是一个强大的RecyclerAdapter框架(什么是RecyclerView?),它能节约开发者大量的开发时间,集成了大部分列表常用需求解决方案。为什么会有它?请查看「Android开源框架BRVAH由来篇」该框架于2016年4月10号发布的第1个版本到现在已经一年多了,经历了800多次代码提交,140多次版本打包,修复了1000多个问题,获得了9000多star,非常感谢大家的使用以及反馈。
137 0
|
2月前
|
编译器 开发工具 Android开发
|
4月前
|
网络协议 Android开发 虚拟化
Android Studio无法运行程序调试程序出现Unable to connect to ADB.Check the Event Log for possible issues.Verify th
Android Studio无法运行程序调试程序出现Unable to connect to ADB.Check the Event Log for possible issues.Verify th
59 0
Android Studio无法运行程序调试程序出现Unable to connect to ADB.Check the Event Log for possible issues.Verify th
|
6月前
|
测试技术 开发工具 数据库
《移动互联网技术》第十一章 Android应用工程案例: 掌握Android系统的需求分析和设计以及 Android项目的程序测试和版本管理方法
《移动互联网技术》第十一章 Android应用工程案例: 掌握Android系统的需求分析和设计以及 Android项目的程序测试和版本管理方法
71 0
|
6月前
|
Android开发
Android推送运行报错的问题
Android推送运行报错的问题
42 2