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. WPF之Binding的使用

    引出: 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件 ...

  2. (六)《Java编程思想》——初始化及类的加载顺序

    package chapter7; /** * 初始化及类的加载顺序:顺序如下 * 1.基类的static变量 * 2.导出类的static变量 * 3.基类的变量 * 4.基类的构造函数 * 5.导 ...

  3. HTML基础总结<链接>

    HTML 超链接(链接) HTML使用标签 <a>来设置超文本链接. 超链接可以是一个字,一个词,或者一组词,也可以是一幅图像,您可以点击这些内容来跳转到新的文档或者当前文档中的某个部分. ...

  4. web笔记

    application: 在tomcat启动过程,会将所有的应用加载进来,会为每一个应用创建一个application对象.这个对象是唯一.但是所有的web应用是互不影响的. like模糊查询 重定向 ...

  5. 学习Android MediaPlayer

    Android Media Playback 原文 The Android multimedia framework includes support for playing variety of c ...

  6. OC基础 文件管理

    OC基础  文件管理 1.文件管理类NSFileManager对象的创建: NSFileManager *fm = [NSFileManager defaultManager]; 2.文件操作: (1 ...

  7. 《Spring敲门砖之基础教程第一季》 第一章 概要介绍

    百度百科say: Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建.简单来说,Spring是一个分层的JavaSE/EEful ...

  8. Sicily 1156. Binary tree

    题目地址:1156. Binary tree 思路: 如何愉快地前序输出呢,要在存储数据的时候根据位置来存放数据! 一开始被自己蠢哭,一直以为第一个输入的就是根结点(例子的祸害呀啊啊啊!!!!),结果 ...

  9. jquery mobile touch 实例

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  10. php json_decode 后,数字对象转换成了 科学计数法 的解决方案

    php json_decode 后,数字对象转换成了 科学计数法 $obj='{"order_id":213477815351175,"buyer":10000 ...