Android实现开机自动运行程序
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call you back!”总结Android框架,真是说到点子上了。理解这句话的含义,许多有关Android平台上实现某种功能的问题,都能迎刃而解。
使用场景:手机开机后,自动运行程序,在屏幕上显示"Hello. Istarted!"字样。
背景知识:当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了。
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?
Android实现开机自动运行程序的更多相关文章
- 【转】]Android实现开机自动运行程序
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call y ...
- /etc/rc.local 与 /etc/init.d Linux 开机自动运行程序
1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里 --------引用---------------------- 在完成 run level 3 ...
- [VC]VC实现开机自动运行程序
有时候,我们需要在计算机启动的时候就启动某些程序,不要人干预.这里,提供一种让程序开机自动运行的方法.见下面代码: BOOL CXXX::SetAutoRun(CString strPath) { C ...
- CentOS开机自动运行程序的脚本
有些时候我们需要在服务器里设置一个脚本,让他一开机就自己启动.方法如下: cd /etc/init.dvi youshell.sh #将youshell.sh修改为你自己的脚本名编写自己的脚本后保 ...
- Linux、UNIX设置开机自动运行命令、脚本配置
一般我们不建议人工部署开机自动启动的脚本.而是建议通过crontab 部署脚本监控,理由如下: 1.自动开机部署脚本不好定位问题,有可能导致主机重启过慢. 2.自动开机部署脚本不好定位问题,有可能导致 ...
- Android---让你的APK程序开机自动运行(转)
转自: http://blog.sina.com.cn/s/blog_72f6e45701014l6t.html 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service. ...
- Android开机自动运行APP——BroadcastReceiver
前言: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll ca ...
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)
#region//开机自动运行 private void CB_Auto_CheckedChanged(object sender, EventArgs e) {//CB_ ...
- WPF 设置程序开机自动运行(+注册表项)
#region 设置程序开机自动运行(+注册表项) RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Micr ...
随机推荐
- Executing a system tool
Executing a system tool The following code example shows the execution of the Buffer tool from the A ...
- axure 6.5 汉化正式版软件及注册码
Axure公司发布了Axure RP 6.5 正式版. 官方主页: http://www.axure.com/news 官方下载: http://www.axure.com/download 视频介绍 ...
- stl map中的lower_bound和 upper_bound
map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_ ...
- JavaScript中的单引号和双引号解决
在使用JavaScript显示消息或者传递字符数据的时候,经常会碰到数据中夹杂单引号(')或者双引号("),这种语句往往会造成JavaScript报错.对此一般采用/'或者/"的解 ...
- 清空iframe的内容
document.getElementById("web").contentWindow.document.body.innerText = "";
- perforce 使用教程(zz)
http://www.perforce.com/documentation/perforce_technical_documentation http://blog.csdn.net/brucexu1 ...
- javascript数组去重的4个方法(转)
原文地址:http://blog.csdn.net/chengxuyuan20100425/article/details/8497277 面试前端必须准备的一个问题:怎样去掉Javascript的A ...
- windows10 Sqlserver卸载 指定账户不存在
在windows卸载程序时,有时会出现因提示“指定的账户不存在”而无法删除,如下: 这时时候要先选择要删除的项目,进行修复后再进行删除就可以正常删除了
- 实现微信小程序的3rd_session
function 3rd_session($len) { $fp = @fopen('/dev/urandom','rb'); $result = ''; if ($fp !== FALSE) { $ ...
- linux ping IP地址与telnet 测试IP端口
ping命令是不能够测试端口的,ping只是测试网络联接状况以及信息包发送和接收状况. 格式:ping IP地址或主机名 [-t] [-a] [-n count] [-l size] 参数含义:-t不 ...