Android service restart method

This article describes the method of service restart for Android. Share with you for your reference. The details are as follows:

When making an app, we may need a background service running all the time, and we need to use the service component.

However, the service may be killed in the following circumstances:

A. The user manually clicks stop. B. Use third-party software (such as 360 mobile guard) for cleaning. Of course, in addition to system services, other processes will be killed unless you contact the mobile phone manufacturer.

At this time, it may be necessary to restart the service. After reading the Internet for a long time, it was mentioned that timer and AlarmManager were used to realize intermittent broadcast sending and start the service (after the registered broadcast was received, start the service). After I realized it, I found that it was still not satisfied in case B.

I installed a card driver assistant on my mobile phone. I found that in case B, after killing, the service started automatically after a period of time. Observe 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

When you see this log, you find that it is android.intent.action.user_ After the broadcast of present, start the service.

android.intent.action.USER_ Unlock the mobile phone screen corresponding to present. Ordinary users can't keep the mobile phone awake if they are sick. Therefore, we can restart the service by receiving this broadcast to keep the service running in the background.

Just register the broadcast in the androidmanifest.xml file, and then paste the broadcast sent when the mobile phone is turned on.

I hope this article will be helpful to your Android program design.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>