Service随系统启动运行
生活随笔
收集整理的这篇文章主要介绍了
Service随系统启动运行
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
Android系统启动时,会发出android.intent.action.BOOT_COMPLETED广播,定义一个类继承自BroadcastReceiver,监听该广播,并在收到该广播时启动Service,就可以实现在系统启动时运行Service。
如定义类BroadReceiver继承自BroadcastReceiver,在Manifest文件中定义:
<receiver android:name=".BroadReceiver"><intent-filter><!-- 过滤系统启动广播 --><action android:name="android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver>该类能够接收到android.intent.action.BOOT_COMPLETED广播。
Java文件:
public class BroadReceiver extends BroadcastReceiver{ public void onReceive(Context context, Intent intent){ // 收到系统启动广播后if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){// 启动MainServiceIntent i1 = new Intent(context, MainService.class);context.startService(i1);}}收到消息后,判断是否是android.intent.action.BOOT_COMPLETED消息,如果是,则用startService()方法启动Service。
转载于:https://www.cnblogs.com/mstk/p/3632623.html
总结
以上是生活随笔为你收集整理的Service随系统启动运行的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Mysql Federated Ser
- 下一篇: SD卡FAT32文件系统格式