Android end of the process method detailed _android

Source: Internet
Author: User
Tags documentation

The example in this article describes the Android end process approach. Share to everyone for your reference, specific as follows:

Recently doing something similar to the Task manager, which has a function that can end other processes through the manager.

There are still a lot of ways to end a process under the Android platform. First, the "End process" here contains two aspects of ending your own process and ending other processes. Find some ways to end a process by looking through the SDK documentation and some information on the web. Here to do some induction and summary, the article part of the information may come from the online already have articles and posts, due to a relatively long time, so if found this article and other articles are identical, please understand.

One, end an activity

To take the initiative to end an activity, (to emphasize, to end an activity, not a process ) is well known, it must be the finish method, which is beyond doubt, And this method will eventually invoke the activity's lifecycle function OnDestroy method, end the current activity, pop the current one from the task stack and activate the next one. Of course, other finish series methods, we do not have a detailed discussion here, interested students can check the SDK documentation.

Ii. forcing an end to the current process

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

1, killprocess (int pid) Example:

Android.os.Process.killProcess (Android.os.Process.myPid ());

This method needs to be explained in detail. In the documentation for the SDK, explain the following:

Kill the process with the given PID. Note So, though this API allows us to request to kill any process based on its PID, the kernel'll still impose D restrictions on which pids your are actually able to kill. Typically this means the process running the caller's packages/application and any additional processes by th At app; Packages sharing a common UID would also be able to kill each of the other ' s processes.

English is not good, can not be accurately translated, so I hope that we understand, lest we cause misunderstanding. My personal interpretation of this sentence is that this method is conditional on its use:

A, the process to be killed and the current process in the same package or application;

b, the process that will be killed is the additional process created by the current application;

c, the common user's UID is shared between the process that was killed and the current process. (The average user here, is relative to the user of root permissions)

2, system.exit (int code) Example:

System.exit (0);

This method can only be used to end the current process itself, and the individual feels that it is possible to force an exit by this method when the program encounters an exception and fails to perform properly.

It is important to note that these two methods cause the process to exit abnormally, which means that the process exits without executing the onpause, OnStop, and OnDestroy methods, and the process is likely to miss the opportunity to save the data. Therefore, these two methods are best used when an exception occurs!

Iii. end of another process

To end another process through a process. In previous versions of the SDK, the method Restartpackage (PackageName) method was used, but it was introduced in the Android development documentation that this function could cause problems (the previous behavior here is no Longer available to applications because it allows them to break other applications by removing their alarms, stopping the IR services, etc., so we recommend a new approach:

void Killbackgroundprocesses (String packagename)

Since this method does not return a value, we do not know whether or not our target process has actually exited. But I've only found a way to end another process at the moment.

Four, exit to the main screen (remember to handle the current process)

This method is also a way to exit the current process. If we create a lot of activity in the process, but do not want to shut down to exit the task on the top of the work, then we can use this method directly. This method is seen by looking through the information on the Internet. Specific reference to this site article "Android Press back key to exit the Android program implementation Method"

Function: When the return key is pressed, it is returned to the main screen with parameter flag_activity_clear_top, which clears out the current activity.

@Override Public
boolean onKeyDown (int keycode, keyevent event) {
    if (keycode = = Keyevent.keycode_back & & Event.getrepeatcount () = 0) {//pressed if is back, without duplicating
        Intent home = new Intent (intent.action_main);
        Home.setflags (intent.flag_activity_clear_top);
        Home.addcategory (intent.category_home);
        StartActivity (home);
    Return Super.onkeydown (KeyCode, event);
}

This approach does end the current process, but with the App Manager tool on the Android platform, using the 2.3.3 emulator, you'll find that the process that just ended is in the form of cached background processes, and the simulator gives an explanation Said it was for the next time to start the process more quickly.

For more information on Android-related content readers can view the site topics: "Android Debugging techniques and common problems solution summary", "Android Development introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

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.