Tips for Android service not to be killed

Service is a component in the Android system. It is similar to the level of activity, but it cannot run by itself, can only run in the background, and can interact with other components.

During Android development, every time startservice (intent) is called, the onstartcommand (intent, int, int) method of the service object will be called, and then some processing will be done in the onstartcommand method. From the official Android documentation, we know that onstartcommand has four int return values. First, let's briefly talk about the role of int return value.

1、 Onstartcommand has four return values:

START_ Sticky: if the service process is killed, keep the service status as the start status, but do not keep the delivered intent object. Then the system will try to re create the service. Since the service state is in the start state, the onstartcommand (intent, int) method will be called after the service is created. If no start command is passed to the service during this period, the parameter intent will be null. START_ NOT_ Sticky: "non viscous". When using this return value, if the service is abnormally killed after onstartcommand is executed, the system will not automatically restart the service.

START_ REDELIVER_ Intent: retransmit intent. When using this return value, if the service is abnormally killed after onstartcommand is executed, the system will automatically restart the service and pass in the value of intent.

START_ STICKY_ COMPATIBILITY:START_ Sticky is a compatible version, but there is no guarantee that the service will restart after being killed.

2、 Create a service that is not killed

1. Rewrite the following method in service. This method has three return values, start_ Sticky (or start_sticky_compatibility) is created automatically after the service is killed

or

2. Restart the service in ondestroy() of the service

3. Create a broadcast

4. Register the broadcast myreceiver and myservice services in androidmanifest.xml

Note: to unlock, start, switch scenes and activate broadcasting, you need to add permissions, such as startup completion, mobile phone status, etc.

Personally test the Android 4.0.4 version of ZTE u795 mobile phone ADB push to the system \ app. Android: persistent = "true" becomes the core program. When 360 kills the process, myreceiver is still effective to ensure the rebirth of the service. Uh

Kill question:

1. Stop service in settings

In the onDestroy method, call startService to restart Service.

2. Force stop application in settings

The capture system broadcasts (the action is Android. Intent. Action. Package_restarted)

3. Kill the running task with the help of a third-party application

Raise the priority of service, program signature, or ADB push to system \ app

Compared with the application under / data / APP, the application under / system / APP enjoys more privileges. For example, if the persistent attribute is set to true in its manifest.xml file, it can be protected from the influence of out of memory killer. For example, the androidmanifest.xml file of the application 'phone':

After setting, the app is promoted to the core level of the system.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>