Android for service permanent residency

Source: Internet
Author: User

To tell the truth, it's a rogue act. But there are times when you have to. such as the Notifyreceiver. Now aside from these ethical things, we're just going to see how it's technically achieved. A service running in the background can have several ways to stop it, the first of which is software exit, the second is to force the system to stop, and the third is to be killed by some security software. Here we focus on the latter two types of permanent residency implementations. Here are a few ideas: 1. Restart the service;2 through some of the system's actions. Restart the service through a series of operations on the service's Ondestory ().

First of all, the most common system action is the system unlock action. When the system is unlocked, a broadcast (action_user_present) is sent. The broadcast receiver will start the service when it receives the broadcast. The specific implementation is shown in the following code:

1  Public classBootbroadcastreceiverextendsBroadcastreceiver {2 3      PublicBootbroadcastreceiver () {4     }5 6 @Override7      Public voidOnReceive (Context context, Intent Intent) {8         if(Intent.ACTION_USER_PRESENT.equals (Intent.getaction ())) {9LOG.D ("PRESENT", "received");TenIntent nintent=NewIntent (); OneNintent.setclass (context, Runningservice.class); A Context.startservice (nintent); -         } -     } the}

After receiving the system unlocked broadcast message (action_user_present), the service is started by intent. The service code is as follows:

1  Public classRunningserviceextendsService {2 3      PublicRunningservice () {4     }5 6 @Override7      Publicibinder onbind (Intent arg0) {8         return NULL;9     }Ten @Override One      Public voidonCreate () { A         Super. OnCreate (); -     } - @Override the      Public voidOnStart (Intent Intent,intStartid) { -         Super. OnStart (Intent, startid); -LOG.D ("Start", "Start"); -     } + @Override -      Public BooleanOnunbind (Intent Intent) { +         return Super. Onunbind (intent); A     } at @Override -      Public voidOnDestroy () { -         Super. OnDestroy (); -Intent intent=NewIntent (); -Intent.setaction ("Restartservice"); -Runningservice. This. Sendbroadcast (intent); in     } -  to}

The second way is to send a broadcast message in the service's Ondestory () method, indicating that the service is going to be shut down, receiving a restart service via the broadcast receiver, and some of the code is given in the above Ondestory (). Here is the code for the broadcast receiver:

1  Public classServicedestorybroadcastreceiverextendsBroadcastreceiver {2 3      PublicServicedestorybroadcastreceiver () {4         //TODO auto-generated Constructor stub5     }6 7 @Override8      Public voidOnReceive (Context context, Intent Intent) {9         if(Intent.getaction (). Equals ("Restartservice")){TenIntent nintent=NewIntent (); OneNintent.setclass (context, Runningservice.class); A Context.startservice (nintent); -         } -     } the  -}

When the broadcast receiver receives a "Restartservice" broadcast, it will restart the service, which ensures that the service resides.

Both of these methods have been verified by myself. In particular, the second, when we click on the system to force stop the service, the service can be automatically restarted. Whether it is possible to restart after being killed by the security software has not been tested.

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.