Several ways to keep Android backstage from being killed

Source: Internet
Author: User

For a variety of reasons, in the development of Android applications will be made to ensure that they have a background always running requirements, how to ensure that the background is always running, not by the system because the memory is low, not killed by the task manager, not by the software, such as software housekeeper killing, etc. is a more difficult problem. Online also have a variety of programs, the author through their own experiment to learn to summarize the 3 can also be the way, recorded as follows. It 's not an absolute guarantee, but it does improve survival.

Mode one: Service binding notification bar becomes front office

Android is implemented in the background generally through service, but the system itself will kill the service in low memory situations. By binding the service to notification, it becomes a user-visible foreground service, which can greatly improve survival.

The implementation is to create a Notification in the Service, and then call void Android.app.Service.startForeground (int id, Notification Notification) Method runs in the foreground. This approach basically guarantees that the service will not be killed in normal operation and after the task bar removes the history task (except Xiaomi and Meizu phones). But 360 of software stewards can still kill.

Mode two: Alarmmanager constantly start service

The principle of this approach is to start the service continuously by a timed alarm, so that the service can be restarted even if it is killed. You can also monitor the network switch, unlock screen and other broadcasts to start the service.

The reference implementation is as follows:

Intent Intent =new Intent (Mcontext, Myservice.class);
Pendingintent sender=pendingintent
. GetService (mcontext, 0, intent, 0);
Alarmmanager alarm= (Alarmmanager) Getsystemservice (Alarm_service);
Alarm.setrepeating (Alarmmanager.rtc_wakeup,system.currenttimemillis (), 5*1000,sender);

This approach basically guarantees that the service will not be killed in normal operation and after the task bar removes the history task (except Xiaomi and Meizu phones). But 360 of software stewards can still kill. There is also the logic of constantly starting to deal with trouble.

Method Three: Start a multi-process on the C layer through the JNI call

This approach is implemented primarily by starting another process at the bottom. I guess the system and the three-party software steward Kill the application process is through the package name related threads and so on to traverse the shutdown. So starting another process in the C language can escape the fate of killing.

The way of thinking is to apply a process fork () by invoking C, and then the C language by using JNI.

This approach basically guarantees that the service will not be killed in normal operation and after the task bar removes the history task (except Xiaomi and Meizu phones). 360 and other software stewards will not clean up. But it's a bit of a hassle to bring jni interactions.

The above 3 in the way can not escape the millet, Meizu mobile phone taskbar cleanup, the reason needs further study.

Several ways to keep Android backstage from being killed

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.