开机启动,也就是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

最新文章

  1. 用Java编程找到两个字符串中共有的字符
  2. JAVA中的单利
  3. 堡垒机环境-jumpserver部署
  4. uva 1631
  5. [转]使用Oracle SQL Developer连接数据库并创建用户
  6. Java---设计模式app小软件汇总应用
  7. Oracle - 使用序列+触发器实现主键自增长
  8. Servlet程序开发-- 过滤器
  9. 网络编程学习笔记(二)基于TCP的Socket编程
  10. pat1091-1100
  11. spring boot maven META-INF/MAINIFEST.MF
  12. Luogu3191 HNOI2007 紧急疏散 二分答案、最大流
  13. P2163 【[SHOI2007]园丁的烦恼】
  14. 【C++ Primer | 19】嵌套类、局部类
  15. 基因/转录本/任意特征 表达定量工具之featureCounts使用方法 | 参数详解
  16. day67
  17. 删数问题(NOI94)
  18. 程序媛计划——python中级课时1
  19. Python 中 logging 日志模块在多进程环境下的使用
  20. 时间序列模式——ARIMA模型

热门文章

  1. 201871010128-杨丽霞《面向对象程序设计(java)》第六-七周学习总结
  2. 使用 Nacos 的 Docker 镜像,启动 Nacos 服务
  3. (day44)前端、HTTP、HTML
  4. 语音识别:从 WaveNet 到 Tacotron,再到 RNN-T
  5. CloudCompare打开pcd文件
  6. 批处理命令 call
  7. AtCoder Grand Contest 035
  8. Intellij IDEA通过Plugins导入vue.js
  9. centos7.5安装java JDK、tomcat、mysql
  10. nodejs的child_process