展会信息港展会大全

android开机启动service
来源:互联网   发布日期:2016-01-13 22:15:53   浏览:1331次  

导读:1.开机启动后系统会发射出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED。 2.构造一个IntentReceiver类,重构其抽象方法onReceive(Context context, Intent intent),在其中......

1.开机启动后系统会发射出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED。 2.构造一个IntentReceiver类,重构其抽象方法onReceive(Context context, Intent intent),在其中启动你想要启动的Service。 3.在AndroidManifest.xml中,首先加入来获得BOOT_COMPLETED的使用许可,然后注册前面重构的IntentReceiver类,在其中加入 ,以使其能捕捉到这个Action。 an example: AndroidManifest.xml: //获得RECEIVE_BOOT_COMPLETED使用许可 Java代码: public class ServiceBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub String action = arg1.getAction(); /*判断是否与action匹配*/ if(action.equals("android.intent.action.BOOT_COMPLETED")) { Intent serviceLauncher = new Intent(arg0, Service.class); arg0.startService(serviceLauncher); Log.i("ServiceBroadcastReceiver", "StockService loaded at start"); } } }

赞助本站

人工智能实验室

相关热词: 开机启动 service

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港