在前面的文章中提到了remote service 的建立過程,現在我們要讓它開機自動啟動
1.在前面代碼的基礎上添加 RemoteServiceBootReceiver.java ,實現一個intent的receiver
package com.fly;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.util.Log;<br />public class RemoteServiceBootReceiver extends BroadcastReceiver {<br />private static final String TAG = "U0fly RemoteServiceBootReceiver";<br />static final String ACTION = "android.intent.action.BOOT_COMPLETED";<br />@Override<br />public void onReceive(Context arg0, Intent arg1) {<br />Log.d(TAG, "Boot completed");<br />// TODO Auto-generated method stub<br />if (arg1.getAction().equals(ACTION)) {<br />// service<br />Intent myintent = new Intent(arg0, RemoteService.class);<br />myintent.setAction("com.fly.RemoteService");<br />arg0.startService(myintent);<br />}<br />}<br />}<br />
2.在AndroidManifast.xml中添加許可權,並註冊一個receiver
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br />package="com.fly" android:versionCode="1" android:versionName="1.0"><br /><application android:icon="@drawable/icon" android:label="@string/app_name"><br /><activity android:name=".RemoteServiceActivity"<br />android:label="@string/app_name"><br /><intent-filter><br /><action android:name="android.intent.action.MAIN" /><br /><category android:name="android.intent.category.LAUNCHER" /><br /></intent-filter><br /></activity><br /><service android:name="RemoteService"><br /><intent-fliter><br /><action android:name="com.fly.RemoteService" /><br /></intent-fliter><br /></service></p><p><receiver android:name=".RemoteServiceBootReceiver"><br /><intent-filter><br /><action android:name="android.intent.action.BOOT_COMPLETED" /><br /></intent-filter><br /></receiver></p><p></application></p><p><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission><br /><uses-sdk android:minSdkVersion="7" /><br /></manifest>
3.下面是一個簡單一實例, 完成開機啟動Activity的功能:
運行效果:
代碼:
BootSayHello.java
package com.fly;<br />import android.app.Activity;<br />import android.os.Bundle;<br />public class BootSayHello extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> }<br />}
BootBroadcastReceiver.java
package com.fly;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />public class BootBroadcastReceiver extends BroadcastReceiver {<br />static final String ACTION = "android.intent.action.BOOT_COMPLETED";<br />@Override<br />public void onReceive(Context context, Intent intent) {<br />// TODO Auto-generated method stub<br />if (intent.getAction().equals(ACTION)) {<br />Intent sayHelloIntent = new Intent(context, BootSayHello.class);<br />sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />context.startActivity(sayHelloIntent);<br />}<br />}<br />}<br />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br />package="com.fly" android:versionCode="1" android:versionName="1.0"><br /><application android:icon="@drawable/icon" android:label="@string/app_name"><br /><activity android:name=".BootSayHello" android:label="@string/app_name"><br /><intent-filter><br /><action android:name="android.intent.action.MAIN" /><br /><category android:name="android.intent.category.LAUNCHER" /><br /></intent-filter><br /></activity><br /><receiver android:name=".BootBroadcastReceiver"><br /><intent-filter><br /><action android:name="android.intent.action.BOOT_COMPLETED" /><br /></intent-filter><br /></receiver><br /></application><br /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission><br /><uses-sdk android:minSdkVersion="7" /><br /></manifest>