1.首先开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次。
2.构造一个IntentReceiver类,重构其抽象方法onReceiveIntent(Context context, Intent intent),在其中启动你想要启动的Service。
3.在AndroidManifest.xml中,首先加入 来获得BOOT_COMPLETED的使用许可,然后注册前面重构的IntentReceiver类,在其中加入 ,以使其能捕捉到这个Action。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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>
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学习笔记-Activity篇 Contents Android学习笔记-Activity篇 1 一、activity创建: 1 二、在manifest.xml中声明activity: 1 三、启动activity 1 3.1 启动不带返回结果的Activity 1 3.2 启动带有返回结果的...
Android自学笔记-15-Activity的生命周期
android笔记-10.09
新版Android开发教程&笔记--基础入门一 新版Android开发教程&笔记--基础入门二 新版Android开发教程&笔记三--环境搭建与解析 新版Android开发教程&笔记四--Dalvik ADB 新版Android开发教程+笔记五--模拟器、应用1、2...
2、Android02--认识Activity 3、Android03--Context和Application 4、Android04--Android服务 5、Android05--Android服务通信 6、Android06--Android广播接收器 7、Android07--Android日志系统 8、Android08--...
Android-x86对于部分笔记本无法启动图形桌面的解决方案 · Issue #23 · openthos/system-analysisAndroidx8
新版Android开发教程&笔记--基础入门一.pdf 新版Android开发教程&笔记--基础入门二.pdf 新版Android开发教程&笔记三--环境搭建与解析.pdf 新版Android开发教程&笔记四--Dalvik ADB.pdf 新版Android开发教程+笔记五--...
Android开发笔记---ListView
Android笔记- 从DEX档案格式看Dalvik的运作.docx
2、启动activity-显示启动和隐式启动: 3、隐式启动自己的activity 4、获取隐启动传递过来的data数据 5、显示启动传递数据 6、禁用掉横竖屏切换 7、开启activity获取返回值-startActivityForResult:
NULL 博文链接:https://coolxing.iteye.com/blog/1222602
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
Android学习笔记-service[参考].pdf
新版Android开发教程及笔记-完整版
http://blog.csdn.net/loveqRc/article/details/54169666 Android学习笔记--Gson笔记
android学习笔记--clip.pdf
Android自学笔记-16-任务栈源代码
android学习笔记--clip参照.pdf
Android学习笔记 > 十年生死两茫茫,不思量,自难忘,华年短暂,陈辞岁月悠悠伤, > 满腔热血已芜荒,展未来,后生强,战战兢兢,如履薄冰心彷徨, > 青丝化雪、鬓角成霜,已是英雄迟暮,人生怎慷慨激昂? 目录 ...
Android自学笔记-11-listview简单例子