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:

  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
  2. <receiver android:name=".OlympicsReceiver" android:label="@string/app_name">
  3. <intent-filter>
  4. <action android:name="android.intent.action.BOOT_COMPLETED" />
  5. <category android:name="android.intent.category.LAUNCHER" />
  6. </intent-filter>
  7. </receiver>
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
  2. <receiver android:name=".OlympicsReceiver" android:label="@string/app_name">
  3. <intent-filter>
  4. <action android:name="android.intent.action.BOOT_COMPLETED" />
  5. <category android:name="android.intent.category.LAUNCHER" />
  6. </intent-filter>
  7. </receiver>

java:

  1. public class OlympicsReceiver extends IntentReceiver
  2. {
  3. /*要接收的intent源*/
  4. static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  5. public void onReceiveIntent(Context context, Intent intent)
  6. {
  7. if (intent.getAction().equals(ACTION))
  8. {
  9. context.startService(new Intent(context,
  10. OlympicsService.class), null);//启动倒计时服务
  11. Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();
  12. }
  13. }
  14. }
  1. public class OlympicsReceiver extends IntentReceiver
  2. {
  3. /*要接收的intent源*/
  4. static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  5. public void onReceiveIntent(Context context, Intent intent)
  6. {
  7. if (intent.getAction().equals(ACTION))
  8. {
  9. context.startService(new Intent(context,
  10. OlympicsService.class), null);//启动倒计时服务
  11. Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();
  12. }
  13. }
  14. }

注意:现在的IntentReceiver已经变为BroadcastReceiver,OnReceiveIntent为onReceive。所以java这边的代码为:

(也可以实现应用程序开机自动启动)
  1. public class OlympicsReceiver extends BroadcastReceiver
  2. {
  3. /*要接收的intent源*/
  4. static final String ACTION = "android.intent.action.BOOT_COMPLETED";
  5. public void onReceive(Context context, Intent intent)
  6. {
  7. if (intent.getAction().equals(ACTION))
  8. {
  9. context.startService(new Intent(context,
  10. OlympicsService.class), null);//启动倒计时服务
  11. Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();
  12. //这边可以添加开机自动启动的应用程序代码
  13. }
  14. }
  15. }

Android怎么让一个service开机自动启动的更多相关文章

  1. Linux下让一个程序开机自动启动

    1.chkconfig但是要在脚本中满足一定的条件(/etc/init.d/)下存在相关服务 2.将启动的程序写入到/etc/rc.local 选择建议: /etc/rc.local可以作为开机启动程 ...

  2. 【转】android如何实现开机自动启动Service或app

    1.今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android. ...

  3. android Service开机启动及debug

    开机启动一个service需要做的工作如下: 1.开发一个receiver用于接收系统广播: public class BootReceiver extends BroadcastReceiver { ...

  4. Delphi XE开发 Android 开机自动启动

    https://blog.csdn.net/tanqth/article/details/74357209 Android 下的广播 在Android下,要让我们开发的APP能在开机时自动启动,必须使 ...

  5. 如何让A20,android开机自动启动C程序【转】

    本文转载自:http://blog.csdn.net/u011258134/article/details/50749174 如何让A20,android开机自动启动C程序 2014-12-26 11 ...

  6. Android App 开机启动画面和开机自动启动APP程序设置

    1.当前比较成熟一点的应用基本上都会在进入应用之显示一个启动界面 如腾讯微博 2.准备元素  需要开机启动的图片一张 3.新建Activity AlphaAnimation动画:控制对象alpha水平 ...

  7. [转] ubuntu16.04添加系统 service, 并设置开机自动启动

    转:https://www.jianshu.com/p/1958878646bd 1. 创建pfly.service文件 2.  执行 systemctl daemon-reload 3. 执行 sy ...

  8. win7中的Uac与开机自动启动(好几种办法,特别是用不带UAC的程序启动UAC程序是一个简单的好办法,写驱动自启动更是了不得)

    在另一篇文章中已经介绍了给Exe加上Uac的方法,在使用的过程中我们会发现,如果把带Uac的Exe写入注册表的Run中,是无法实现开机自动启动的,原因就是带Uac的exe需要申请管理员权限,以便运行执 ...

  9. Android开发 设置开机自动启动

    原文:http://blog.csdn.net/kevinmeng_ini58/article/details/7700786 片段一: <!-- 开机启动 --> <receive ...

随机推荐

  1. Linux系统中C&Cpp程序开发(一)

    之前一直在Windows系统下进行程序的设计,近期开始学习使用Linux系统,因而打算将程序开发也转移到Linux系统下.今天先简单介绍一下该系统下的C程序开发步骤. 首先要预先安装vim和gcc工具 ...

  2. NPM与调试工具的使用

    1)NPM 2)nodemon 自动监视文件的变化并重启应用 3)pm2 启动node,资源共享 4)node-inspector node调试工具 5)Chrome Developer Tools

  3. 内容观察者 ContentObserver 监听短信、通话记录数据库 挂断来电

    Activity public class MainActivity extends ListActivity {     private TextView tv_info;     private  ...

  4. error BC31019 无法写入输出文件 未指定错误

    今天获取项目最后版本后,编译突然出现错误 error BC31019 无法写入输出文件 "xxx目录" 未指定错误 试着调整当前用户对这个文件的读写权限等各种方法,都未能解决该问题 ...

  5. php+js 瀑布流源码

    官方网站:更多源码 新浪微博:QQ公众号 QQ:各种源码 602902342 大牛技术群: 452207697 下载地址:http://pan.baidu.com/s/1bnNipI3 密码: h93 ...

  6. 第1章 网络编程基础(3)——基本Socket通信

    服务器常用模型

  7. Python subprocess执行持续输出shell命令的控制

    研究了大半天,为了获取持续输出的shell指令结果,并对结果进行分析,一直因为无法控制subprocess开启的子进程头疼,研究了半天,参考众多大神的博客后,终于实现,目前已时间为控制点,在实际业务中 ...

  8. Spark学习笔记--Transformation 和 action

    转自:http://my.oschina.net/hanzhankang/blog/200275 附:各种操作的逻辑执行图 https://github.com/JerryLead/SparkInte ...

  9. LeetCode_Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  10. smarty 从配置文件读取变量

    smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...