Android process management: How to terminate the process during development

Android process management: How to terminate the process during development

In Android, directly killing the application process is usually not a recommended practice, but sometimes you may need to kill the corresponding process for certain specific needs (such as debugging or managing applications).

  1. 「Use android.os.Process.killProcess() method」: Use android.os.Process.myPid() method to get the ID of the current process, and then use android.os.Process.killProcess() method to kill the process. Due to Android's security mechanism, only processes with the same UID can kill each other. So this method can only be used for suicide, that is, to kill the calling process.
 int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid);
  1. 「Use System.exit() method」: You can terminate the currently running Java virtual machine, thereby terminating the program. When the Java virtual machine is terminated, all running threads will be stopped immediately, including non-daemon threads, and the resources occupied by activities will also be released. Calling this method will only terminate the current Java virtual machine and will not directly affect other Android processes.
 System.exit(0);

or:

 private static final int MSG_DELAY_EXIT_APP = 0; private static Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case MSG_DELAY_EXIT_APP: Runtime.getRuntime().exit(0); break; } } }; mHandler.sendEmptyMessageDelayed(MSG_DELAY_EXIT_APP, 4000);
  1. "Use ActivityManager.killBackgroundProcesses() method": You can force close all background processes associated with the specified package name (it will not kill the foreground process), and it will only work when system resources are tight, and the KILL_BACKGROUND_PROCESSES permission is required. This method can only be used for killing others, that is, killing the processes of other applications, and cannot be used for suicide.
 String packageName = getPackageName(); // 获取当前应用的包名ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); am.killBackgroundProcesses(packageName);

The Android system has its own strategy for managing processes and memory. If there is enough memory, Android will not kill any process at will; but if there is insufficient memory, the process may be killed at any time. When there is enough memory, Android will try to restore the previously killed process. For application developers, you should try to avoid relying on static variables to store important data, but should save the data to files or other persistent storage. At the same time, you also need to pay attention to the reasonable management of the application's memory usage to avoid the process being killed by the system due to problems such as memory leaks.

<<:  ActivityThread and ApplicationThread, the bridge between the main thread of the Android application and AMS communication

>>:  LinearLayoutCompat makes your Android linear layout more compatible, flexible and consistent

Recommend

Time is running out for Windows Phone to turn around

Microsoft has been particularly active recently, ...

WeChat's major update: Video accounts can now post 1-hour long videos

With the cheap price of traffic, video has become...

The copy targeting is the same, why is the CTR still lower than others?

The copywriting is obviously very well written, s...

Why does the skin age?

Anti-aging can be said to be one of the mainstrea...

ZTE explains how to access the Internet on airplanes

Regarding the "Air China provides in-flight b...

Annoying phone battery life: It’s not the battery’s fault

The functions of the smartphones we use nowadays ...

The entire process of a new APP from establishment to acquiring users!

In the early stages of entrepreneurship , from bu...

Console-exclusive games are coming to PC. Where will TV games go?

Recently, there have been media reports that the ...

How should SEM bidders deal with the setbacks they may encounter in their work?

As an SEM person, you will encounter various prob...

Why is Sichuan pepper so spicy?

Spicy and spicy. Spicy and spicy are always assoc...