android开机自启动app
转自:http://my.oschina.net/jgy/blog/135858
http://www.trinea.cn/android/android-boot_completed-not-work/
前言:
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service。怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call you back!”总结Android框架,真是说到点子上了。理解这句话的含义,许多有关Android平台上实现某种功能的问题,都能迎刃而解。
使用场景:
手机开机后,自动运行程序。
{只是最近本人在做万达大歌星点餐系统,需要用到系统启动直接启动App避免服务员玩别的app才研究的,以下分享是本人参考网络,亲自尝试成功的代码,贴出来与大家分享,希望能帮助更多的人!不懂的可以问我哦。QQ:11745405 }
背景知识:
当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到这个消息,再启动之即可。记住,Android框架说:Don't call me, I'll call you back。我们要做的是等到接收这个消息,而实现的手段就是实现一个BroadcastReceiver。
代码解析:
1、界面MainActivity.java
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.*; import android.widget.Toast; public class MainActivity extends Activity { // 收到消息启动此Activity @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); super .setIntegerProperty( "splashscreen" , R.drawable.wandapad_splash); TextView tv = new TextView( this ); tv.setText( "Hello这是MainActivity" ); setContentView(tv); } |
2、接收广播消息:BootBroadcastReceiver.java
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.example; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; /** * Created with IntelliJ IDEA. * User: 郏高阳 * Date: 13-6-5 * Time: 下午8:25 * To change this template use File | Settings | File Templates. */ 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 mainActivityIntent = new Intent(context, MainActivity. class ); // 要启动的Activity mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mainActivityIntent); } } } |
3、配置文件:AndroidManifest.xml
在application内添加一下配置:
?
1 2 3 4 5 | < receiver android:name = ".BootBroadcastReceiver" > < intent-filter > < action android:name = "android.intent.action.BOOT_COMPLETED" /> intent-filter > receiver > |
向系统注册了一个receiver,子节点intent-filter表示接收android.intent.action.BOOT_COMPLETED消息。
4、在manifest中添加以下权限
?
1 | < uses-permission android:name = "android.permission.RECEIVE_BOOT_COMPLETED" > uses-permission > |
延伸思考:
在多数情况下,要自动运行的不是有界面的程序,而是在后台运行的service。此时,就要用startService来启动相应的service了。
2、自启动失败的原因
接收不到BOOT_COMPLETED广播可能的原因
(1)、BOOT_COMPLETED对应的action和uses-permission没有一起添加
(2)、应用安装到了sd卡内,安装在sd卡内的应用是收不到BOOT_COMPLETED广播的
(3)、系统开启了Fast Boot模式,这种模式下系统启动并不会发送BOOT_COMPLETED广播
(4)、应用程序安装后重来没有启动过,这种情况下应用程序接收不到任何广播,包括BOOT_COMPLETED、ACTION_PACKAGE_ADDED、CONNECTIVITY_ACTION等等。
Android3.1之后,系统为了加强了安全性控制,应用程序安装后或是(设置)应用管理中被强制关闭后处于stopped状态,在这种状态下接收不到任何广播,除非广播带有FLAG_INCLUDE_STOPPED_PACKAGES标志,而默认所有系统广播都是FLAG_EXCLUDE_STOPPED_PACKAGES的,所以就没法通过系统广播自启动了。所以Android3.1之后
(1)、应用程序无法在安装后自己启动
(2)、没有ui的程序必须通过其他应用激活才能启动,如它的Activity、Service、Content Provider被其他应用调用。
存在一种例外,就是应用程序被adb push you.apk /system/app/下是会自动启动的,不处于stopped状态。
具体说明见:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
3、adb发送BOOT_COMPLETED
我们可以通过
1 | adb shell am broadcast - a android . intent . action . BOOT_COMPLETED |
命令发送BOOT_COMPLETED广播,而不用重启测试机或模拟器来测试BOOT_COMPLETED广播,这条命令可以更精确的发送到某个package,如下:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n package_name/class_name1 | adb shell am broadcast - a android . intent . action . BOOT_COMPLETED - c android . intent . category . HOME - n package_name / class_name |
更多相关文章
- Android(安卓)Wifi模块分析(三)
- Android四大基本组件介绍与生命周期
- 第三章 Android程序设计基础
- Android(安卓)Activity的启动
- Android(安卓)任务和回退堆栈---启动任务
- Android——Activity四种启动模式
- 修复Android(安卓)App在开机后入口类被启动两次的bug
- android手机客户端上传文件,java servlet服务器端接收并保存到服
- android 命令(adb shell)进入指定模拟器或设备