<Android Studio> 2.APP开机启动
开机启动,也就是App随着机器开机而启动,在很多工业场景中是非常常见的。
开机启动的基本原理就是监听系统启动相关的广播,然后启动App。
为了实现开机启动,我人为的分为几个步骤
1.创建broadcastReceiver接收器
2.AndroidManifest.xml中配置权限和注册接收器接收的广播消息类型
3.接收器中代码实现
--------------------------------------------------------------------
1.创建broadcastReceiver接收器

2.AndroidManifest.xml中配置android.permission.RECEIVE_BOOT_COMPLETED权限,见前文
在自动生成的<receiver>标签中,添加过滤器
<receiver
android:name=".receiver.PlayerReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
<action android:name="android.intent.action.MEDIA_EJECT"/>
<action android:name="android.intent.action.MEDIA_REMOVED"/>
<data android:scheme="file" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
过滤器
过滤器为啥要这样定义呢,原因是
Android API Level8以上的时候,程序可以安装在SD卡上。如果程序安装在SD卡上,那么在BOOT_COMPLETED广播发送之后,SD卡才会挂载,因此程序无法监听到BOOT_COMPLETED广播。
此时,同时监听开机和sd卡挂载就可以了。
如果BOOT_COMPLETED和MEDIA_MOUNTED,MEDIA_EJECT写在同一个intent-filter中,那么无法检测到BOOT_COMPLETED,对于没有SD卡的手机,只能检测BOOT_COMPLETED,这样就会导致无法检测到开机了。
所以,分别放到了两个intent-filter中。
3.接收器中代码实现
下面的代码会执行多次,如果只想运行一次,只需要在代码中增加判断,通过ActivityManager获取当前运行的后台任务,然后获取激活的Activity名称,
判断是否是本程序的主Activity来实现单例运行。此时,需要追加权限<uses-permission android:name="android.permission.GET_TASKS"/>
public class PlayerReceiver extends BroadcastReceiver {
    private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
    private final String ACTION_MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED";
    private final String ACTION_MEDIA_UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED";
    private final String ACTION_MEDIA_EJECT = "android.intent.action.MEDIA_EJECT";
    private final String ACTION_MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("logs::", intent.getAction());
        Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
        /**
         * 如果 系统 启动的消息,则启动 APP 主页活动
         */
        if (    ACTION_BOOT.equals(intent.getAction()) ||
                ACTION_MEDIA_MOUNTED.equals(intent.getAction()) ||
                ACTION_MEDIA_UNMOUNTED.equals(intent.getAction()) ||
                ACTION_MEDIA_EJECT.equals(intent.getAction()) ||
                ACTION_MEDIA_REMOVED.equals(intent.getAction())
            ) {
            Intent intentMainActivity = new Intent(context, MainActivity.class);
            intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intentMainActivity);
            Toast.makeText(context, "开机完毕~", Toast.LENGTH_LONG).show();
        }
    }
}
效果:

附录:
一、ACTION列表
常用ACTION如下:
| 
 String  | 
 "android.intent.action.ADD_SHORTCUT"  | 
 动作:在系统中添加一个快捷方式。.  | 
| 
 String  | 
 "android.intent.action.ALL_APPS"  | 
 动作:列举所有可用的应用。  | 
| 
 String  | 
 "android.intent.action.ANSWER"  | 
 动作:处理拨入的电话。  | 
| 
 String  | 
 "android.intent.action.BUG_REPORT"  | 
 动作:显示 activity 报告错误。  | 
| 
 String  | 
 "android.intent.action.CALL"  | 
 动作:拨打电话,被呼叫的联系人在数据中指定。  | 
| 
 String  | 
 "android.intent.action.CLEAR_CREDENTIALS"  | 
 动作:清除登陆凭证 (credential)。  | 
| 
 String  | 
 "android.intent.action.VIEW"  | 
 动作:和 VIEW_ACTION 相同,是在数据上执行的标准动作。  | 
| 
 String  | 
 "android.intent.action.DELETE"  | 
 动作:从容器中删除给定的数据。  | 
| 
 String  | 
 "android.intent.action.DIAL"  | 
 动作:拨打数据中指定的电话号码。  | 
| 
 String  | 
 "android.intent.action.EDIT"  | 
 动作:为制定的数据显示可编辑界面。  | 
| 
 String  | 
 "android.intent.action.EMERGENCY_DIAL"  | 
 动作:拨打紧急电话号码。  | 
| 
 String  | 
 "android.intent.action.LOGIN"  | 
 动作:获取登录凭证。  | 
| 
 String  | 
 "android.intent.action.MAIN"  | 
 动作:作为主入口点启动,不需要数据。  | 
| 
 String  | 
 "android.intent.action.PICK"  | 
 动作:从数据中选择一个项目item,将被选中的项目返回。  | 
| 
 String  | 
 "android.intent.action.PICK_ACTIVITY"  | 
 动作:选择一个activity,返回被选择的activity的类名  | 
| 
 String  | 
 "android.intent.action.RUN"  | 
 动作:运行数据(指定的应用),无论它(应用)是什么。  | 
| 
 String  | 
 "android.intent.action.SENDTO"  | 
 动作:向 data 指定的接收者发送一个消息。  | 
| 
 String  | 
 "android.intent.action.GET_CONTENT"  | 
 动作:让用户选择数据并返回。  | 
| 
 String  | 
 "android.intent.action.INSERT"  | 
 动作:在容器中插入一个空项 (item)。  | 
| 
 String  | 
 "android.intent.action.SETTINGS"  | 
 动作:显示系统设置。输入:无。  | 
| 
 String  | 
 "android.intent.action.VIEW"  | 
 动作:向用户显示数据。  | 
| 
 String  | 
 "android.intent.action.WALLPAPER_SETTINGS"  | 
 动作:显示选择墙纸的设置界面。输入:无。  | 
| 
 String  | 
 "android.intent.action.WEB_SEARCH"  | 
 动作:执行 web 搜索。  | 
| 
 String  | 
 "android.intent.action.SYNC"  | 
 动作:执行数据同步。  | 
| 
 String  | 
 "android.intent.action.SERVICE_STATE"  | 
 广播:电话服务的状态已经改变。  | 
| 
 String  | 
 "android.intent.action.TIMEZONE_CHANGED"  | 
 广播:时区已经改变。  | 
| 
 String  | 
 "android.intent.action.TIME_SET"  | 
 广播:时间已经改变(重新设置)。  | 
| 
 String  | 
 "android.intent.action.TIME_TICK"  | 
 广播:当前时间已经变化(正常的时间流逝)。  | 
| 
 String  | 
 "android.intent.action.UMS_CONNECTED"  | 
 广播:设备进入 USB 大容量存储模式。  | 
| 
 String  | 
 "android.intent.action.UMS_DISCONNECTED"  | 
 广播:设备从 USB 大容量存储模式退出。  | 
| 
 String  | 
 "android.intent.action.WALLPAPER_CHANGED"  | 
 广播:系统的墙纸已经改变。  | 
| 
 String  | 
 "android.intent.action.XMPP_CONNECTED"  | 
 广播:XMPP 连接已经被建立。  | 
| 
 String  | 
 "android.intent.action.XMPP_DI  | 
 广播:XMPP 连接已经被断开。  | 
| 
 String  | 
 "android.intent.action.SIG_STR"  | 
 广播:电话的信号强度已经改变。  | 
| 
 String  | 
 "android.intent.action.BATTERY_CHANGED"  | 
 广播:充电状态,或者电池的电量发生变化。  | 
| 
 String  | 
 "android.intent.action.BOOT_COMPLETED"  | 
 广播:在系统启动后,这个动作被广播一次(只有一次)  | 
| 
 String  | 
 "android.intent.action.DATA_ACTIVITY"  | 
 广播:电话的数据活动(data activity)状态已经改变  | 
| 
 String  | 
 "android.intent.action.DATA_STATE"  | 
 广播:电话的数据连接状态已经改变。  | 
| 
 String  | 
 "android.intent.action.DATE_CHANGED"  | 
 广播:日期被改变。  | 
| 
 String  | 
 "android.server.checkin.FOTA_CANCEL"  | 
 广播:取消所有被挂起的 (pending) 更新下载。  | 
| 
 String  | 
 "android.server.checkin.FOTA_INSTALL"  | 
 广播:更新已经被确认,马上就要开始安装。  | 
| 
 String  | 
 "android.server.checkin.FOTA_READY"  | 
 广播:更新已经被下载,可以开始安装。  | 
| 
 String  | 
 "android.server.checkin.FOTA_RESTART"  | 
 广播:恢复已经停止的更新下载。  | 
| 
 String  | 
 "android.server.checkin.FOTA_UPDATE"  | 
 广播:通过 OTA 下载并安装操作系统更新。  | 
| 
 String  | 
 "android.intent.action.MEDIABUTTON"  | 
 广播:用户按下了“Media Button”。  | 
| 
 String  | 
 "android.intent.action.MEDIA_BAD_REMOVAL"  | 
 广播:扩展卡从SD卡插槽拔出,但是挂载点还没unmount。  | 
| 
 String  | 
 "android.intent.action.MEDIA_EJECT"  | 
 广播:用户想要移除扩展介质(拔掉扩展卡)。  | 
| 
 String  | 
 "android.intent.action.MEDIA_MOUNTED"  | 
 广播:扩展介质被插入,而且已经被挂载。  | 
| 
 String  | 
 "android.intent.action.MEDIA_REMOVED"  | 
 广播:扩展介质被移除。  | 
| 
 String  | 
 "android.intent.action.MEDIA_SCANNER_FINISHED"  | 
 广播:已经扫描完介质的一个目录。  | 
| 
 String  | 
 "android.intent.action.MEDIA_SCANNER_STARTED"  | 
 广播:开始扫描介质的一个目录。  | 
| 
 String  | 
 "android.intent.action.MEDIA_SHARED"  | 
 广播:扩展介质的挂载被解除 (unmount)  | 
| 
 String  | 
 "android.intent.action.MEDIA_UNMOUNTED"  | 
 广播:扩展介质存在,但是还没有被挂载 (mount)。  | 
| 
 String  | 
 "android.intent.action.MWI"  | 
 广播:电话的消息等待(语音邮件)状态已经改变。  | 
| 
 String  | 
 "android.intent.action.PACKAGE_ADDED"  | 
 广播:设备上新安装了一个应用程序包。  | 
| 
 String  | 
 "android.intent.action.PACKAGE_REMOVED"  | 
 广播:设备上删除了一个应用程序包。  | 
| 
 String  | 
 "android.intent.action.PHONE_STATE"  | 
 广播:电话状态已经改变。  | 
| 
 String  | 
 "android.intent.action.PROVIDER_CHANGED"  | 
 广播:更新将要(真正)被安装。  | 
| 
 String  | 
 "android.intent.action.PROVISIONING_CHECK"  | 
 广播:要求provisioning service下载最新的设置  | 
| 
 String  | 
 "android.intent.action.SCREEN_OFF"  | 
 广播:屏幕被关闭。  | 
| 
 String  | 
 "android.intent.action.SCREEN_ON"  | 
 广播:屏幕已经被打开。  | 
| 
 String  | 
 "android.intent.action.NETWORK_TICKLE_RECEIVED"  | 
 广播:设备收到了新的网络 "tickle" 通知。  | 
| 
 String  | 
 "android.intent.action.STATISTICS_REPORT"  | 
 广播:要求 receivers 报告自己的统计信息。  | 
| 
 String  | 
 "android.intent.action.STATISTICS_STATE_CHANGED"  | 
 广播:统计信息服务的状态已经改变。  | 
| 
 String  | 
 "android.intent.action.CFF"  | 
 广播:语音电话的呼叫转移状态已经改变。  | 
| 
 String  | 
 "android.intent.action.CONFIGURATION_CHANGED"  | 
 广播:设备的配置信息已经改变,参见 Resources.Configuration  | 
| 
 String  | 
 "android.intent.category.ALTERNATIVE"  | 
 类别:说明activity是用户正在浏的数据的一个可选操作。  | 
| 
 String  | 
 "android.intent.category.WALLPAPER"  | 
 类别:这个 activity 能过为设备设置墙纸。  | 
| 
 String  | 
 "android.intent.category.UNIT_TEST"  | 
 类别:应该被用作单元测试(通过 test harness 运行)。  | 
| 
 String  | 
 "android.intent.category.TEST"  | 
 类别:作为测试目的使用,不是正常的用户体验的一部分。  | 
| 
 String  | 
 "android.intent.category.TAB"  | 
 类别:activity应该在TabActivity中作为一个tab使用  | 
| 
 String  | 
 "android.intent.category.SAMPLE_CODE"  | 
 类别:To be used as an sample code example (not part of the normal user experience).  | 
| 
 String  | 
 "android.intent.category.PREFERENCE"  | 
 类别:activity是一个设置面板 (preference panel)。  | 
| 
 String  | 
 "android.intent.category.HOME"  | 
 类别:主屏幕 (activity),设备启动后显示的第一个 activity。  | 
| 
 String  | 
 "android.intent.category.BROWSABLE"  | 
 类别:能够被浏览器安全使用的 activities 必须支持这个类别。  | 
| 
 String  | 
 "android.intent.category.DEFAULT"  | 
 类别:如果 activity 是对数据执行确省动作(点击, center press)的一个选项,需要设置这个类别。  | 
| 
 String  | 
 "android.intent.category.DEVELOPMENT_PREFERENCE"  | 
 类别:说明 activity 是一个设置面板 (development preference panel).  | 
| 
 String  | 
 "android.intent.category.EMBED"  | 
 类别:能够在上级(父)activity 中运行。  | 
| 
 String  | 
 "android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST"  | 
 类别:To be used as code under test for framework instrumentation tests.  | 
| 
 String  | 
 "android.intent.category.GADGET"  | 
 类别:这个 activity 可以被嵌入宿主 activity (activity that is hosting gadgets)。  | 
| 
 String  | 
 "android.intent.category.LAUNCHER"  | 
 类别:Activity 应该被显示在顶级的 launcher 中。  | 
| 
 String  | 
 "android.intent.category.SELECTED_ALTERNATIVE"  | 
 类别:对于被用户选中的数据,activity 是它的一个可选操作。  | 
| 
 int  | 
 8 0x00000008  | 
 启动标记:和 NEW_TASK_LAUNCH 联合使用,禁止将已有的任务改变为前景任务 (foreground)。  | 
| 
 int  | 
 4 0x00000004  | 
 启动标记:设置以后,activity 将成为历史堆栈中的第一个新任务(栈顶)。  | 
| 
 int  | 
 1 0x00000001  | 
 启动标记:设置以后,新的 activity 不会被保存在历史堆栈中。  | 
| 
 int  | 
 2 0x00000002  | 
 启动标记:设置以后,如果 activity 已经启动,而且位于历史堆栈的顶端,将不再启动(不重新启动) activity。  | 
| 
 int  | 
 16 0x00000010  | 
 启动标记:如果这个标记被设置,而且被一个已经存在的 activity 用来启动新的 activity,已有 activity 的回复目标 (reply target) 会被转移给新的 activity 。  | 
| 
 String  | 
 "android.intent.extra.INTENT"  | 
 附加数据:和 PICK_ACTIVITY_ACTION 一起使用时,说明用户选择的用来显示的 activity;和 ADD_SHORTCUT_ACTION 一起使用的时候,描述要添加的快捷方式。  | 
| 
 String  | 
 "android.intent.extra.LABEL"  | 
 附加数据:大写字母开头的字符标签,和 ADD_SHORTCUT_ACTION 一起使用。  | 
| 
 String  | 
 "android.intent.extra.TEMPLATE"  | 
 附加数据:新记录的初始化模板。  | 
二、ACTION类别归类
标准ACTION:
ACTION_MAIN 作为一个主要的进入口,而并不期望去接受数据
ACTION_VIEW 向用户去显示数据
ACTION_ATTACH_DATA 别用于指定一些数据应该附属于一些其他的地方,例如,图片数据应该附属于联系人
ACTION_EDIT 访问已给的数据,提供明确的可编辑
ACTION_PICK 从数据中选择一个子项目,并返回你所选中的项目
ACTION_CHOOSER 显示一个activity选择器,允许用户在进程之前选择他们想要的
ACTION_GET_CONTENT 允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)
ACTION_DIAL 拨打一个指定的号码,显示一个带有号码的用户界面,允许用户去启动呼叫
ACTION_CALL 根据指定的数据执行一次呼叫
(ACTION_CALL在应用中启动一次呼叫有缺陷,多数应用ACTION_DIAL,ACTION_CALL不能用在紧急呼叫上,紧急呼叫可以用ACTION_DIAL来实现)
ACTION_SEND 传递数据,被传送的数据没有指定,接收的action请求用户发数据
ACTION_SENDTO 发送一跳信息到指定的某人
ACTION_ANSWER 处理一个打进电话呼叫
ACTION_INSERT 插入一条空项目到已给的容器
ACTION_DELETE 从容器中删除已给的数据
ACTION_RUN 运行数据,无论怎么
ACTION_SYNC 同步执行一个数据
ACTION_PICK_ACTIVITY 为已知的Intent选择一个Activity,返回别选中的类
ACTION_SEARCH 执行一次搜索
ACTION_WEB_SEARCH 执行一次web搜索
ACTION_FACTORY_TEST 工场测试的主要进入点,
标准广播Action:
ACTION_TIME_TICK 当前时间改变,每分钟都发送,不能通过组件声明来接收,只有通过Context.registerReceiver()方法来注册
ACTION_TIME_CHANGED 时间被设置
ACTION_TIMEZONE_CHANGED 时间区改变
ACTION_BOOT_COMPLETED 系统完成启动后,一次广播
ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)
ACTION_PACKAGE_CHANGED 一个已存在的应用程序包已经改变,包括包名
ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)
ACTION_PACKAGE_RESTARTED 用户重新开始一个包,包的所有进程将被杀死,所有与其联系的运行时间状态应该被移除,包括包名(重新开始包程序不能接收到这个广播)
ACTION_PACKAGE_DATA_CLEARED 用户已经清楚一个包的数据,包括包名(清除包程序不能接收到这个广播)
ACTION_BATTERY_CHANGED 电池的充电状态、电荷级别改变,不能通过组建声明接收这个广播,只有通过Context.registerReceiver()注册
ACTION_UID_REMOVED 一个用户ID已经从系统中移除
Google官方参考地址:
http://developer.android.com/reference/android/content/Intent.html
参考资料:
http://blog.csdn.net/ithomer/article/details/8242471
<Android Studio> 2.APP开机启动的更多相关文章
- 【Android Studio安装部署系列】十八、Android studio更换APP应用图标
		
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio新建项目后会有一个默认图标,那么如何更换图标呢? 替换图标 这个方案不建议直接在已有项目上更换图标,建议 ...
 - Android App 开机启动画面和开机自动启动APP程序设置
		
1.当前比较成熟一点的应用基本上都会在进入应用之显示一个启动界面 如腾讯微博 2.准备元素 需要开机启动的图片一张 3.新建Activity AlphaAnimation动画:控制对象alpha水平 ...
 - 【Android】Mac下Android Studio设置App启动页
		
先将启动页放到项目资源中,图片一般是1080*1920的jpg. 新建一个activity,如图: 创建成功之后,打开刚刚创建的activity,来进行代码的编写: public class BZLa ...
 - 二 Android Studio 打包EgretApp (开机画面、横竖屏、调试、和原生交互)
		
测试环境: Windows7 Egret Engine 5.0.14 Egret support 5.0.12 Android Studio 2.3 目录: 一 修改开机画面 二 横竖屏设置 三 修改 ...
 - 我的Android进阶之旅------>关于使用Android Studio替换App的launcher图标之后仍然显示默认的ic_launcher图标的解决方法
		
前言 最近做了一个App,之前开发该App的时候一直以来都是默认的launcher图标启动的, 今天美工换了一个App的launcher 图标,因此在Android Studio中将默认的lanche ...
 - 【风马一族_Android】Android Studio 给APP设置签名
		
在Android Studio中,给App签名,如果没有给App设置签名的话,Android Studio会主动给app设置一个默认的签名 接下来,介绍主动给App设置一个签名的整个步骤过程: 1) ...
 - [App]Android Studio First App
		
准备着看Android Studio的体验如何. 通过Android Studio构建一个默认的项目,添加一些元素 <RelativeLayout xmlns:android="htt ...
 - Android环境结构Android Studio解决方法不能启动,第一次
		
android Studio 下载和eclipse 的android开发环境的搭建下载包: 联系: http://pan.baidu.com/s/1kTKJZkN password: qxqf And ...
 - Android Studio 安装后首次启动的 Config path ...... is invalid 问题(转)
		
原文链接:http://m.blog.csdn.net/blog/hnust_xiehonghao/46127775 1. 问题描述: 安装好Android Studio后,启动时弹出如下信息: Co ...
 
随机推荐
- 用python提取xml里面的链接源码
			
因群里朋友需要提取xml地图里面的链接,就写了这个程序. 代码: #coding=utf-8 import urllib import urllib.request import re url='ht ...
 - docker 部署appium
			
1.docker appium镜像和命令:https://github.com/appium/appium-docker-android 2.启动appium docker run --privile ...
 - Vue之挂载点、变量、事件、js对象、文本指令、过滤器、事件指令和属性指令
			
1.vue导入-挂载点 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&qu ...
 - djang小项目过程中的小问题 02(跳转界面)
			
我觉着自己生下来就是解决问题的 ##1. 今天在使用登录注册模板时,输入后缀index,正常显示登录界面,但是点击 立即注册 之后不会跳转到注册页面 因为我观察到后缀名发生变化了,但是出发点是错的,前 ...
 - js replace方法第二个参数,远不止你想的那么强大
			
js replace() 方法,想必大家都不陌生. 定义和用法: replace()方法用于在字符串中用一些字符替换另一些字符,或者替换一个与正则表达式匹配的子串. stringObject.repl ...
 - 【oracle】迁表结构和数据
			
背景:把一些表和数据从某库迁到另一个库 1.命令框: exp yktsh/yktsh_2019@orcl30 file=d:\yktsh20191201.dmp log=d:\daochu; exp ...
 - js判断超过几个字符后显示省略号
 - About me & 友链
			
窝是图图小淘气 面对世界很好奇 啊呸 抱歉 拿错咧 重来 是他!是他!就是他! 我们滴朋友哦小哪吒! (汗 又拿错咧 菜鸡yxj 是来自美丽富饶的SDGR 的一名高中生 每天最喜欢做的事就是 花式被机 ...
 - [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表
			
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
 - Codeforces 1204D Kirk and a Binary String - 数学
			
题目传送门 传送门 群除我均会猜结论/找规律,sad.... 以下内容只保证代码能过system test,证明应该都是在纯口胡 约定下文中的$LIS$表示最长不下降子序列. 定义$zero(s)$表 ...