在前面的文章中提到了remote service 的创建过程,现在我们要让它开机自动启动
1.在前面代码的基础上添加 RemoteServiceBootReceiver.java ,实现一个intent的receiver
- 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
- <?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>