How to implement service restart with Android _android

Source: Internet
Author: User

The example in this article describes how Android implements service restart. Share to everyone for your reference. Specifically as follows:

When we do the app, we may need a backend service that runs all the time and uses the service component.

But services may be killed in the following situations:

A. User clicks stop manually.
B. The use of Third-party software (such as 360 mobile phone guards) to clean up, of course, this way in addition to system services, other processes will certainly be killed, unless with the handset manufacturer contact.

At this time may need to restart the service, read the internet for half a day, mentioned with timer, Alarmmanager to achieve intermittent send broadcast start service (registered broadcast received, start service), I realized the next, found still in the case of B is still not satisfied.

I installed a card driver assistant, found in the case of B, after killing, the service has been automatically started, watch log printing.

07-12 14:12:15.735:i/hadeslee (1456): Receiver,action=android.intent.action.user_present
07-12 14:12:15.745:i/hadeslee (1456): Kakaservice.oncreate .....
07-12 14:12:15.745:i/hadeslee (1456): kakaservice.onstartcommand,flags=2,startid=1
07-12 14:12:15.755:i/activitymanager (218): Start proc Com.miui.weather2 for broadcast Com.miui.weather2/.service. serviceupdateweather:pid=1484 uid=10060 gids={3003}
07-12 14:12:15.755:i/hadeslee (1456): Nextremindtime=null

Seeing this log, it was found that the service was started after the broadcast of Android.intent.action.USER_PRESENT was received.

Android.intent.action.USER_PRESENT corresponding to the phone screen unlock, the general user can not be sick of the phone has been in the wake of the state, so we can receive this broadcast service restart, keep the service has been running in the background.

Register the broadcast in the Androidmanifest.xml file OK, incidentally affixed to the mobile phone boot sent broadcast.

<receiver android:name= "Com.agilemobi.comac.collect.android.services.UserPresentReceiver" > 
  < intent-filter> 
 <action android:name= "Android.intent.action.USER_PRESENT"/> 
  </intent-filter > 
</receiver> 
<receiver android:name= " Com.agilemobi.comac.collect.android.services.BootReceiver "> 
  <intent-filter> 
 <action Android : Name= "Android.intent.action.BOOT_COMPLETED"/> 
 <category android:name= "Android.intent.category.HOME"/ > 
  </intent-filter> 
</receiver>
public class Userpresentreceiver extends Broadcastreceiver {
  private static final String TAG = "Userpresentreceiver";
  @Override public
  void OnReceive (context context, Intent Intent) {
    //TODO auto-generated stub 
    LOG.E (TAG, "receive broadcast");
    Do something
  }
}

I hope this article will help you with your Android program.

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.