Android starts early in third-party application system applications, kills its own active third-party applications to restart

Source: Internet
Author: User

1. Why are third-party applications likely to start earlier than the system app?

Android can be consulted, here is not detailed, here does not elaborate rom boot and bootloader. The approximate flow of software startup should be

    • Start kernel
    • Execute servicemanager to start up some native services with commands (including WiFi, power, Rild, Surfaceflinger, mediaserver, etc.)
    • Start the first process in Dalivk zygote, start the Java Layer System services System_server (including Powermanagerservice, Activitymanagerservice, Telephony.registry, Displaymanagerservice, etc.) these parts of the service are associated with the services of native
    • Launches Luncher and persistent apps, which are system-level declared android:persistent= "true" in Androidmanifest.xml
    • Issue action_boot_completed broadcasts to other applications.

It is important to note here that the app that declares the Android:persistent property to True is killed or will be restarted on its own initiative. In the system we know that the app with the Android:persistent attribute true must have the phone app, which means that third-party apps should start at least later than the phone app, how do you infer it? The simplest way to look at the size of its PID, the smaller the PID value starts first. Third-party apps can be launched before the phone app. We explore its application of the Androidmanifest.xml (PS: How to look at the APK code, online have you understand the apktool, etc.), The properties of the intent-filter that find its static receiver defined in Androidmanifest such as the following:

       <receiver android:name= "Com.anguanjia.safe.AAAReceiver" > <intent-filter android:priority= "21474                83647 "></span> <action android:name=" Android.bluetooth.adapter.action.STATE_CHANGED "/> <action android:name= "Android.net.wifi.WIFI_STATE_CHANGED"/> <action android:name= "and Roid.net.conn.CONNECTIVITY_CHANGE "/> <action android:name=" Android.intent.action.ANY_DATA_STATE "/&G                T <action android:name= "Android.net.wifi.STATE_CHANGE"/> </intent-filter> <intent-filt                Er android:priority= "2147483647" > <action android:name= "Android.intent.action.MEDIA_UNMOUNTED"/> <action android:name= "Android.intent.action.MEDIA_MOUNTED"/> <action android:name= "Android.intent.action.MEDIA_REMOVED"/> <action android:name= "Android.intent.action.MEDIA_CHECKING" />               <action android:name= "Android.intent.action.MEDIA_EJECT"/> <data android:scheme= "f Ile "/> </intent-filter>

2147483647 What is this value? Very big, oh, it is the largest value of int!

Let's look at the Google Docs

Android:priority
The priority that should is given to the parent component with regard to handling intents of the type described by the FIL ter. This attribute have meaning for both activities and broadcast receivers:

It provides information about what able an activity was to respond to an intent that matches the filter, relative to other a Ctivities that could also respond to the intent. When an intent could is handled by multiple activities with different priorities, Android would consider only those with hi Gher priority values as potential targets for the intent.

It controls the order in which broadcast receivers is executed to receive broadcast messages. Those with higher priority values is called before those with lower values. (The order applies only to synchronous messages; it's ignored for asynchronous messages.)

Use this attribute only if you really need to impose a specific order in which the broadcasts is received, or want to for Ce Android to prefer one activity over others.

The value must is an integer, such as "100". Higher numbers has a higher priority. The default value is 0. The value must is greater than-1000 and less than 1000.

This value is the priority of receiver, and the higher the value, the greater the priority, which runs in order of precedence, but the document introduction priority value size is-1000~1000. The maximum value of the int is applied, but the android:priority value is not checked by the Android platform. The intent-filter of the application receiver has the highest priority after booting, and the intent in the filter is sent by the system (Android.intent.action.MEDIA_MOUNTE, Android.net.wifi.WIFI_STATE_CHANGED and so on), this time the app will be launched based on this intent.

It is important to note that the receiver is static and must be registered in a androidmanifest.

A similar message is sent when the WiFi is successfully registered and a wifi_state_changed message is sent, or after some other parts have been completed. These messages are sent earlier than the system-level app that has the property persistent, and this can happen if the third-party app starts earlier than the system-level app.


2. On Android phones Why do I want to shut down the program completely? There is a theory that Android phones in order to have a better user experience. will be in the background to actively launch some programs. This way our front desk will feel more smooth and smooth when operating.

However, assume that the program executes too much. Excessive overhead on CPU memory, often causes the system to use the slower, even the phone hangs the problem, in memory management This fast Android has two mechanisms to solve the problem, one is in the framework layer in the Trimapplication method to achieve, The other one is the Lowmemorykiller in the kernel. There is no longer a detailed statement.
But for the user. I just want to completely shut down third-party programs to avoid excessive use of my traffic or secretly do something I do not want. There seems to be no way to close, then why? Let me tell you about one of these things.
Service as the name implies. Execution in front of the background can be able to execute in the current process is also able to execute in other processes, the service can be used for multiple apps sharing, is implemented through the binder mechanism. When I kill a process with a service (without calling StopService ()), the app will reboot itself after a while. The following is the order in which the code is called. View from bottom to top.

Com.android.server.am.ActiveServices.scheduleServiceRestartLocked (Activeservices.java)

Com.android.server.am.ActiveServices.killServicesLocked (Activeservices.java)

Com.android.server.am.ActivityManagerService.cleanUpApplicationRecordLocked (Activitymanagerservice.java)

Com.android.server.am.ActivityManagerService.handleAppDiedLocked (Activitymanagerservice.java)

Com.android.server.am.ActivityManagerService.appDiedLocked (Activitymanagerservice.java)

Com.android.server.am.activitymanagerservice$appdeathrecipient.binderdied (Activitymanagerservice.java)

From the code flow we see that the service is restart. The process is also started on the service, and the service executes in the process of restarting.

In such circumstances is not really a way to do it, of course not. Assuming we overwrite the Onstartcommand function in the service and the return value is start_not_sticky, we do not voluntarily reboot after we kill the process, and the application we want to close can be completely closed. It will not be restarted on its own initiative.

    public int Onstartcommand (Intent Intent, int flags, int startid) {        return start_not_sticky;    }

Framwork Implementation Code

Frameworks/base/services/java/com/android/server/am/activeservices.java Case Service.start_not_sticky:                        {//We are do with the associated start arguments.                        R.finddeliveredstart (Startid, true);                            if (r.getlaststartid () = = Startid) {//There is no more work, and the This service                            doesn ' t want to hang around if killed.   R.stopifkilled = true;                    The variable is set to True} break; } if (sr.startrequested && (sr.stopifkilled | | canceled)) {//Enter into this condition if (Sr.                        Pendingstarts.size () = = 0) {sr.startrequested = false;                                    if (sr.tracker! = null) {sr.tracker.setStarted (False, mAm.mProcessStats.getMemFactorLocked (), Systemclock.uptimemilLis ());                             } if (!sr.hasautocreateconnections ()) {//Whoops, no reason to restart!  Bringdownservicelocked (SR); Run over here. Do not restart the app}}}

Override the Onstartcommand method and return the code invocation order with a value of Start_not_sticky, which is viewed from the bottom up.

Com.android.server.am.ActiveServices.bringDownServiceLocked (Activeservices.java)

Com.android.server.am.ActiveServices.killServicesLocked (Activeservices.java)

Com.android.server.am.ActivityManagerService.cleanUpApplicationRecordLocked (Activitymanagerservice.java)

Com.android.server.am.ActivityManagerService.handleAppDiedLocked (Activitymanagerservice.java)

com.android.server.am.ActivityManagerService.appDiedLocked (Activi Tymanagerservice.java)

Com.android.server.am.activitymanagerservice$appdeathrecipient.binderdied (Activitymanagerservice.java)

A description of the Onstartcommand return value is attached to Google doc

For started services, there is, additional major modes of operation they can decide to run in, depending on the value They return from Onstartcommand (): START_STICKY was used for services, was explicitly started and stopped as needed, while START_NOT_STICKY Or is START_REDELIVER_INTENT used for services this should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.

Here is an explanation of this return value:

When the service process is killed for some reason (insufficient memory, forced shutdown, etc.), Start_sticky again, after the system has created enough memory service, in Onstartcommand in handle it is null intent.

Start_not_sticky notifies the system that the service will not be created again. Another return value start_redeliver_intent the service and accompanies the original intent go processing.

Android starts early in third-party application system applications, kills its own active third-party applications to restart

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.