Android Boot auto Start service

Source: Internet
Author: User

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?
  1. Package com.fly;
  2. Import Android.content.BroadcastReceiver;
  3. Import Android.content.Context;
  4. Import android.content.Intent;
  5. Import Android.util.Log;
  6. Public class Remoteservicebootreceiver extends Broadcastreceiver {
  7. private static final String TAG = "U0fly remoteservicebootreceiver";
  8. static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
  9. @Override
  10. public void OnReceive (Context arg0, Intent arg1) {
  11. LOG.D (TAG, "Boot completed");
  12. //TODO auto-generated method stub
  13. if (arg1.getaction (). Equals (ACTION)) {
  14. //Service
  15. Intent myintent = new Intent (arg0, Remoteservice.   Class);
  16. Myintent.setaction ("Com.fly.RemoteService");
  17. Arg0.startservice (myintent);
  18. }
  19. }
  20. }

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?
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
  3. package= "com.fly" android:versioncode="1" android:versionname="1.0" >
  4. <application android:icon="@drawable/icon" android:label="@string/app_name" >
  5. <activity android:name=". Remoteserviceactivity "
  6. Android:label="@string/app_name" >
  7. <intent-filter>
  8. <action android:name="Android.intent.action.MAIN"/>
  9. <category android:name="Android.intent.category.LAUNCHER"/>
  10. </intent-filter>
  11. </activity>
  12. <service android:name="Remoteservice" >
  13. <intent-fliter>
  14. <action android:name="Com.fly.RemoteService"/>
  15. </intent-fliter>
  16. </service>
  17. <receiver android:name=". Remoteservicebootreceiver ">
  18. <intent-filter>
  19. <action android:name="Android.intent.action.BOOT_COMPLETED"/>
  20. </intent-filter>
  21. </receiver>
  22. </application>
  23. <uses-permission android:name="Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission>
  24. <USES-SDK android:minsdkversion="7"/>
  25. </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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.