Auto-start and maintenance of the Background SERVICE Process of Android SERVICE

Source: Internet
Author: User

Service components are often used in android development. They are often used as backend services and must always run and be responsible for handling tasks that are not necessarily human. Some security software, such as 360, may end the process. If the Service is not maintained, it will be killed.

How to maintain the running status of the Service is explained now. The core is to use the ANDROID system broadcast. This resident program that will not be affected by other software will trigger its own program to check the running status of the Service, if it is killed, it will be restarted.

The system broadcast I use is Intent. ACTION_TIME_TICK. This broadcast is sent once every minute. We can check the running status of the Service once every minute. If the broadcast has ended, restart the Service.

Below are the specific code and precautions:

1. Use of Intent. ACTION_TIME_TICK

We know that broadcast registration includes static registration and dynamic registration, but this system broadcast can only be used through dynamic registration. That is, you cannot receive this broadcast by registering in manifest. xml. You can only register the broadcast in the code using the registerReceiver () method.

Register broadcast in ThisApp extends Application:

 
 
  1. IntentFilter filter = newIntentFilter(Intent.ACTION_TIME_TICK); 
  2. MyBroadcastReceiver receiver = new MyBroadcastReceiver(); 
  3. registerReceiver(receiver, filter); 

In the onReceive of the broadcast receiver MyBroadcastReceiver extends BroadcastReceiver

 
 
  1. If (intent. getAction (). equals (Intent. ACTION_TIME_TICK )){
  2. // Check the Service status
  3. }

2. Check and start the Service

 
 
  1. Boolean isServiceRunning = false;
  2. ActivityManager manager = (ActivityManager) ThisApp. getContext (). getSystemService (Context. ACTIVITY_SERVICE );
  3. For (RunningServiceInfo service: manager. getRunningServices (Integer. MAX_VALUE )){
  4. If ("so. xxxx. WidgetUpdateService". equals (service. service. getClassName ()))
  5. // Service class name
  6. {
  7. IsServiceRunning = true;
  8. }
  9. }
  10. If (! IsServiceRunning ){
  11. Intent I = new Intent (context, WidgetUpdateService. class );
  12. Context. startService (I );
  13. }

Another topic is Service startup.

Similar to the preceding implementation, the Service is started by monitoring the system broadcast of the startup. However, after you perform the above check, you will not start the Service after boot, because the above program will start the Service in a minute or two. The Code is as follows:

 
 
  1. if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
  2. Intent i = new Intent(context, LogService.class); 
  3.     context.startService(i); 
  4.     } 

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.