Daemon for Android Service daemon

Source: Internet
Author: User

service components are often encountered in Android development, often as a back-office service that needs to be kept running at all times and is responsible for handling some of the necessary (see no-man) tasks. Some security software, such as 360, will have the ability to end the process, if you do not maintain the service, it will be killed.

in the early days, we could pass the


1. The service overrides the Onstartcommand method, this method has three return values, Start_sticky is the service is killed after the automatic


public int Onstartcommand (Intent Intent, int flags, int startid) {

return start_sticky;  

}



2. Configure android:persistent= "true"


3. Setforeground (true);


4. Android:process= "Com.xxx.xxxservice" configuration into a separate process


The above approach either increases service priority or survival, and does not solve the problem of being forcibly killed by security software.

Either running the service as a fourth separate process in the 360 old version is possible, but will still be killed in a newer version of 360.



How to keep the service running state is now to be explained, the core is to use the Android system broadcast, trigger their own program to check the running status of the service, if killed, then up again.


commonly used has the power-on broadcast, unlocks the screen the broadcast, the electricity change and so on, among them the broadcast of unlocking is more frequent, but also does not guarantee certain frequency, especially in the certain time (such as the user sleeps, the user does not unlock the operation). And we still have to do something, there's no way. .


Therefore, I have adopted a different scheme. In addition, two similar to a daemon service, check the service's running status, register the response broadcast, to protect it, once found not running it to start.





the system broadcast I'm using is


Intent.action_time_tick, this broadcast is sent every minute, we can check the service running status every minute, and if it has been ended, restart the service.


its advantage is that the interval is short and very stable, and other broadcasts do not guarantee this, of course, in the specific application is to use according to demand, combined with other broadcasts to ensure that their service will be restarted.

After all, security software is becoming more and more powerful, the update is also very frequent. There is still time to see if there are other methods, combined with a few to use.


below is the specific code and considerations:


1, the use of Intent.action_time_tick


we know that broadcast registration has static registration and dynamic registration, but this system broadcast can only be used in the way of dynamic registration. That is, you cannot receive this broadcast by registering in Manifest.xml, only through the code.


Registerreceiver () method registration.


in the Thisapp extends application or register the broadcast in the service:
   
[Java]  View Plain Copy
    1. intentfilter filter = Newintentfilter (Intent.action_time_tick);
    2.       
    3. mybroadcastreceiver receiver = new mybroadcastreceiver ();
    4. Registerreceiver (receiver, filter);

in the onreceive of the broadcast receiver Mybroadcastreceiver extends Broadcastreceiver.
    
[Java]  View Plain Copy
  1. Boolean isservicerunning = false;
  2.   
  3.   
  4. if (Intent.getaction (). Equals (Intent.action_time_tick)) {
  5.         
  6. //Check service status
  7.         
  8. Activitymanager manager = (Activitymanager) appapplication.getcontext (). Getsystemservice (context.activity_   SERVICE);
  9. For (runningserviceinfo service:manager.getRunningServices (Integer.max_value)) {
  10. if("So.xxxx.xxxxService". Equals (Service.service.getClassName ()))
  11.             
  12.      {   
  13. isservicerunning = true;
  14.     }   
  15.         
  16.      }   
  17. if (!isservicerunning) {
  18. Intent i = new Intent (context, Xxxservice. class   );
  19. Context.startservice (i);
  20.     }   
  21.   
  22.   
  23. }   


The Ultimate Solution : Use JNI, the C-end fork process to detect if the service is alive, and restart the service if the service has been killed.

Daemon for Android Service daemon

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.