Android 开机自动启动服务
在前面的文章中提到了remote service 的創(chuàng)建過程,現(xiàn)在我們要讓它開機自動啟動
?
1.在前面代碼的基礎上添加 RemoteServiceBootReceiver.java ,實現(xiàn)一個intent的receiver
[java] view plaincopyprint?package com.fly; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class RemoteServiceBootReceiver extends BroadcastReceiver { private static final String TAG = "U0fly RemoteServiceBootReceiver"; static final String ACTION = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context arg0, Intent arg1) { Log.d(TAG, "Boot completed"); // TODO Auto-generated method stub if (arg1.getAction().equals(ACTION)) { // service Intent myintent = new Intent(arg0, RemoteService.class); myintent.setAction("com.fly.RemoteService"); arg0.startService(myintent); } } } ?
?
2.在AndroidManifast.xml中添加權限,并注冊一個receiver
[java] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fly" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".RemoteServiceActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="RemoteService"> <intent-fliter> <action android:name="com.fly.RemoteService" /> </intent-fliter> </service> <receiver android:name=".RemoteServiceBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <uses-sdk android:minSdkVersion="7" /> </manifest> ?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/xin36933/p/3796952.html
總結(jié)
以上是生活随笔為你收集整理的Android 开机自动启动服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 访问者(Visitor)模式
- 下一篇: C++11之thread线程