SoFunction
Updated on 2025-03-11

Detailed explanation of how to end the process on Android

This article describes the method of ending the process by Android. Share it for your reference, as follows:

Recently I am working on something similar to the task manager, which has a function that can end other processes through this manager.

On the Android platform, there are still many ways to end the process. First of all, it is pointed out that the "end process" here includes two aspects: ending one's own process and ending other processes. By checking the SDK documents and some online information, you can find some ways to end the process by yourself. I will summarize and summarize here. Some information in the article may come from existing articles and posts on the Internet. Since it has been a long time, if you find that this article is similar to other articles, please understand.

1. End an activity

To proactively end an activity, (First, it is to end an activity, not a process) As we all know, it must be the finish method, so there is no doubt about this. Moreover, this method will call the Activity's life cycle function onDestroy method, end the current Activity, pop up the current Activity from the task stack, and activate the next Activity. Of course, we will not discuss the other finish series methods in detail here. Interested students can check the SDK documentation by themselves.

2. Force the end of the current process

There are two ways to force the end of the current process. (Haha, here is the end of the process)

1. KillProcess(int pid) Example:

(());

This method needs to be explained in detail. In the SDK documentation, the explanation is as follows:

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

The English is not good and cannot be translated accurately, so I hope everyone can understand it for themselves so as not to cause misunderstandings to everyone. My personal explanation for this sentence is that this method is conditional:

a. The process to be killed is in the same package or application as the current process;

b. The process to be killed is an additional process created by the current application;

c. The process to be killed and the current process share the UID of the ordinary user. (The ordinary users here are relative to users with Root permissions)

2. (int code) Example:

(0);

This method can only be used to end the current process itself. I personally think that when the program encounters an exception and cannot be executed normally, you can force exit through this method.

It should be noted that these two methods will cause the process to exit abnormally, that is, the process will not execute the onPause, onStop and onDestroy methods when exiting, so the process is likely to miss the opportunity to save data. Therefore, these two methods are best used when an exception occurs!

3. End another process

One process needs to end another process. In previous SDK versions, the method restartPackage(packageName) method has been used, but in the Android development documentation, it is said that this function will cause some problems (the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc.), so it is recommended that you use a new method:

void killBackgroundProcesses(String packageName)

Since this method does not return a value, we don't know whether our target process actually exits. However, I've only found this method that can end another process.

4. Exit to the home screen (remember it is the processing of the current process)

This method is also a way to exit the current process. If we create many activities in the process, but do not want to exit the activity that is not at the top of the task stack when we do not want to close it, then we can use this method directly. This method is seen by looking up online information. For details, please refer to the article on this site "How to implement Android press the back key to exit Android program

Function: When the return key is pressed, it returns to the home screen and comes with the parameter FLAG_ACTIVITY_CLEAR_TOP, which will clean up the current activity.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && () == 0) { // If the pressed BACK is not repeated at the same time        Intent home = new Intent(Intent.ACTION_MAIN);
        (Intent.FLAG_ACTIVITY_CLEAR_TOP);
        (Intent.CATEGORY_HOME);
        startActivity(home);
    }
    return (keyCode, event);
}

This method does end the current process, but if you check it through the App Manager tool on the Android platform, use the 2.3.3 emulator and you will find that the process that has just been terminated will exist in the form of Cached background Process, and the emulator gives an explanation, saying that it is to start the process faster next time.

For more information about Android related content, please check out the topic of this site:Android debugging skills and solutions to common problems》、《Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.