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 ...
随机推荐
- apache ab工具对网站进行压力测试
Apache -- ab工具主要测试网站的(并发性能) 这个工具非常的强大. 基本语法 : cmd>ab.exe –n 请求总次数 -c 并发数 请求页面的url 进入到ab.ex ...
- ASP.NET MVC Controller接收ajax post方式发送过来的json对象或数组数据
本例旨在说明我的一种Controller接收ajax提交(POST)过来的json对象或数组信息的方式,感觉应该有更好的方式,欢迎提出宝贵意见. JSON.stringify(jsonObj)不支持I ...
- 使用DataSet数据集删除记录
使用DataSet删除记录和使用DataSet更新记录非常的相似,DataSet删除记录的步骤如下所示. q 创建一个Connection对象. q 创建一个DataAdapter对象. q 初 ...
- USB OTG to PC USB API简介
本API分为四部分:Linux或Android内核 (主要是gadget驱动).linux端API及其DEMO.Windows 驱动.Windows API及其Demo. 一.linux.Androi ...
- Implement Insert and Delete of Tri-nay Tree
问题 Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but wi ...
- Hibernate插入数据效率测试
硬件配置: 4G内存.CPUi3-2.3 数据库SQL2008 package com.pan.test; import org.hibernate.Session; import org.hiber ...
- C# 汉子增加UTF-8头
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Conv ...
- java学习笔记 (7) —— 实现简单的上传处理
1.下载apache 的 commons-fileupload.jar 包及 commons-io.jar 2.引入到lib 文件夹下 3.新建uploadApache.jsp <%@ page ...
- HTML&CSS基础学习笔记1.13—无序列表
无序列表 有时我们的工作繁忙,杂事很多,怕忘记,就会把事情一件件列出来,防止忘记. 它们的排列顺序对于我们来说并不重要,可以随意调换,我们将它称为无序列表,HTML里用<ul>标签来表示无 ...
- openpyxl
openpyxl库的使用,这个处理xlsx还是挺有用的 ref:传送门 from openpyxl import Workbook from openpyxl import load_workbook ...