In the previous article, we mentioned the creation of the remote service, and now we have to start it automatically.
1. Add Remoteservicebootreceiver.java based on the previous code to implement a 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);
- }
- }
- }
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.BO Ot_completed "; @Overridepublic void OnReceive (Context arg0, Intent arg1) {log.d (TAG," Boot completed ");//TODO Auto-generated Method Stubif (Arg1.getaction (). Equals (ACTION)) {//Serviceintent myintent = new Intent (arg0, Remoteservice.class); Myintent.setaction ("Com.fly.RemoteService"); Arg0.startservice (myintent) ;}}
2. Add the permissions in the Androidmanifast.xml and register a 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>
<?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>