Three ways to get your service out of "one-click acceleration" and system kill

Source: Internet
Author: User

Basically everyone knows that increasing the service priority can largely keep your service from being killed due to insufficient memory, but the system simply kills the lower priority at this point, assuming that the memory is not enough and will kill your service.
1.android:persistent= "true"

The resident memory attribute is not valid for third-party apps. Here is the official note

Android:persistent

Whether or not the application should remain running @ all times-' true ' if it should, and ' false ' if not. The default value is "false". Applications should not normally set this flag; Persistence mode is a intended only for certain system applications.


2.startForeground place it in the foreground

Notification Notification = new Notification ();  Notification.flags = notification.flag_ongoing_event;  Notification.flags |= notification.flag_no_clear;  Notification.flags |= Notification.flag_foreground_service;  

3. Ability to monitor intent.action_time_tic system clock broadcasts. The system sends this broadcast every once in a while. When the service is killed, the broadcast starts over a period of time
Dynamic BrochureAndroid.intent.action.TIME_TICK Monitoring

Infer whether the service is started

public boolean isservicerunning (String serviceName) {Activitymanager manager = (Activitymanager) getsystemservice ( Context.activity_service);     For (Runningserviceinfo service:manager.getRunningServices (integer.max_value))     {     if (servicename.equals ( Service.service.getClassName ()))    {     return true;     }     }    return false;}

After receiving the broadcast, infer whether to start the service if it does not start it

    if (Intent.getaction (). Equals (Intent.action_time_tick))    {if (!isservicerunning (name)) {Intent mintent = new Intent (context, myservice.class); Context.startservice (mintent); }    }




Three ways to get your service out of "one-click acceleration" and system kill

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.