Android怎么让一个service开机自动启动
1.首先开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次。
2.构造一个IntentReceiver类,重构其抽象方法onReceiveIntent(Context context, Intent intent),在其中启动你想要启动的Service。
3.在AndroidManifest.xml中,首先加入<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>来获得BOOT_COMPLETED的使用许可,然后注册前面重构的IntentReceiver类,在其<intent-filter>中加入<action android:name="android.intent.action.BOOT_COMPLETED" /> ,以使其能捕捉到这个Action。
一个例子
xml:
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
- <receiver android:name=".OlympicsReceiver" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </receiver>
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
- <receiver android:name=".OlympicsReceiver" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </receiver>
java:
- public class OlympicsReceiver extends IntentReceiver
- {
- /*要接收的intent源*/
- static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- public void onReceiveIntent(Context context, Intent intent)
- {
- if (intent.getAction().equals(ACTION))
- {
- context.startService(new Intent(context,
- OlympicsService.class), null);//启动倒计时服务
- Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();
- }
- }
- }
- public class OlympicsReceiver extends IntentReceiver
- {
- /*要接收的intent源*/
- static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- public void onReceiveIntent(Context context, Intent intent)
- {
- if (intent.getAction().equals(ACTION))
- {
- context.startService(new Intent(context,
- OlympicsService.class), null);//启动倒计时服务
- Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();
- }
- }
- }
注意:现在的IntentReceiver已经变为BroadcastReceiver,OnReceiveIntent为onReceive。所以java这边的代码为:
- public class OlympicsReceiver extends BroadcastReceiver
- {
- /*要接收的intent源*/
- static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- public void onReceive(Context context, Intent intent)
- {
- if (intent.getAction().equals(ACTION))
- {
- context.startService(new Intent(context,
- OlympicsService.class), null);//启动倒计时服务
- Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();
- //这边可以添加开机自动启动的应用程序代码
- }
- }
- }
Android怎么让一个service开机自动启动的更多相关文章
- Linux下让一个程序开机自动启动
1.chkconfig但是要在脚本中满足一定的条件(/etc/init.d/)下存在相关服务 2.将启动的程序写入到/etc/rc.local 选择建议: /etc/rc.local可以作为开机启动程 ...
- 【转】android如何实现开机自动启动Service或app
1.今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android. ...
- android Service开机启动及debug
开机启动一个service需要做的工作如下: 1.开发一个receiver用于接收系统广播: public class BootReceiver extends BroadcastReceiver { ...
- Delphi XE开发 Android 开机自动启动
https://blog.csdn.net/tanqth/article/details/74357209 Android 下的广播 在Android下,要让我们开发的APP能在开机时自动启动,必须使 ...
- 如何让A20,android开机自动启动C程序【转】
本文转载自:http://blog.csdn.net/u011258134/article/details/50749174 如何让A20,android开机自动启动C程序 2014-12-26 11 ...
- Android App 开机启动画面和开机自动启动APP程序设置
1.当前比较成熟一点的应用基本上都会在进入应用之显示一个启动界面 如腾讯微博 2.准备元素 需要开机启动的图片一张 3.新建Activity AlphaAnimation动画:控制对象alpha水平 ...
- [转] ubuntu16.04添加系统 service, 并设置开机自动启动
转:https://www.jianshu.com/p/1958878646bd 1. 创建pfly.service文件 2. 执行 systemctl daemon-reload 3. 执行 sy ...
- win7中的Uac与开机自动启动(好几种办法,特别是用不带UAC的程序启动UAC程序是一个简单的好办法,写驱动自启动更是了不得)
在另一篇文章中已经介绍了给Exe加上Uac的方法,在使用的过程中我们会发现,如果把带Uac的Exe写入注册表的Run中,是无法实现开机自动启动的,原因就是带Uac的exe需要申请管理员权限,以便运行执 ...
- Android开发 设置开机自动启动
原文:http://blog.csdn.net/kevinmeng_ini58/article/details/7700786 片段一: <!-- 开机启动 --> <receive ...
随机推荐
- vb的LINQ实现
vb实现LINQ非常简单的例子: Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7} Dim allNumbers = From number In nu ...
- wed网页开发面试笔试必备小知识
HTML中行内元素与块级元素的区别: 在标准文档流里面,块级元素具有以下特点: ①总是在新行上开始,占据一整行: ②高度,行高以及外边距和内边距都可控制: ③宽带始终是与浏览器宽度一样,与内容无关: ...
- CSS Hack是什么意思
CSS hack由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效 ...
- document.onclick vs window.onclick
The JavaScript Window object is the highest level JavaScript object which corresponds to the web bro ...
- 安装Node.js
1.window下安装Node.js 安装git,方便使用命令行. 网址:http://www.git-scm.com/download/ 下载后直接安装即可 接着安装Node.js https:// ...
- 自定义标签(JSTL)
自定义标签的步骤: 1.确定需求,如:用<my:date/>输出当前时间 2.编写Java类:需要实现实现接口javax.servlet.jsp.tagext.JspTag 具体的接口为: ...
- [转载]C++ 堆与栈简单的介绍
在C和C++中,有三种使用存储区的基本方式: [静态存储区(Static Memory)] 在静态存储区中,连接器(linker)根据程序的需求为对象分配空间.全局变量.静态类成员以及函数中的静态 ...
- ajax初学
//参数说明url:请求文件地址//fnSucc:请求成功执行的函数,请求成功的条件为readyState状态码为4:且status状态码为200,status状态为浏览器请求返回的状态码//在fnS ...
- php DOMDocument 递归 格式化缩进HTML文档
function format(\DOMNode $node, $treeIndex = 0) { //不格式化的标签 if (in_array($node->nodeName, array(& ...
- flask_socketio 使用范例
最近看flask有关的文档.想security, sqlalchemy, jwt等等相关的插件都没有问题.唯独这个socketio, 跟着官方文档一起蒙, 如果不看github上面的范例还真不好懂. ...