Perfect solution for Android full exit program

Source: Internet
Author: User

Background: The hypothesis has two activity, Activity1 and Activity2, 1 jump to 2, if you want to exit the program in 2, the general online is more common saying is to use

System.exit (0)

Or

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

But the actual application is not able to really quit, the problem is? 1 Jump to 2 o'clock, if Activity1 you finish off, two is can quit the program, but sometimes 1 jump to 2 o'clock, we can not Activity1 finish off, then Activity2 can not quit the program. So there is a better way to exit the program, whether you have a finish before a Activiy can quit the program, or

Final Activitymanager am = (activitymanager) getsystemservice (Context.activity_service); Am.restartpackage (Getpackagename ());

But remember to add permissions in the Androidmanifest.xml.

<uses-permission android:name= "Android.permission.RESTART_PACKAGES" >
</uses-permission>
The above methods can be used before the 2.1 version, but after 2.2 all obsolete, after tossing, finally found the ultimate solution:

Intent Startmain = new Intent (intent.action_main); Startmain.addcategory (Intent.category_home); Startmain.setflags (Intent.flag_activity_new_task); StartActivity (Startmain); System.exit (0);

As for why Google cancels out the restartpackage (), see below:
Android principle, why don't we need to close the program manually
Do not care about the size of the remaining memory, in fact, many people are using other systems to bring the habit of coming. Android most applications do not exit the design actually makes sense, this and the system on the process of scheduling mechanism has a relationship. If you know Java, you can get a clearer idea of this mechanism. In fact, similar to Java's garbage collection mechanism, the system has a rule to reclaim memory. There is a threshold for memory scheduling, only below this value the system will press a list to shut down what the user does not need. Of course this value is set very small by default, so you will see memory hovering in very few numbers. But in fact he doesn't affect speed. Instead, speed up the next time you start the app. This is one of the advantages that Android has advertised, and it's not necessary to artificially close the process. Especially the software that uses the auto-off process. (This solves the misunderstanding that everybody has to close the process!) )
Someone here will say, why is it slow to run a large program when memory is low? In fact, it is very simple, in memory for a long time to open a large program, it will trigger the system itself to adjust the process scheduling policy, which is very consumption of system resources, especially when a program frequently to the system to request memory. In this case, the system does not shut down all open processes, but selectively shuts down, and frequent scheduling naturally slows down the system. Therefore, the forum has a change in the memory threshold of the program can be a certain improvement. However, changes can also cause some problems, depending on the setting of the value.
So, is there any need for process management software? Some. Just before you run a large program, you can manually turn off some processes to free up memory, which can significantly improve your running speed. However, some small programs can be completely managed by the system itself.
When it comes to this, some friends may ask if the program is not going to consume more power. I'll just talk about the fundamentals of Android backstage, and you'll see. When the Android app is switched to the background, it is actually paused, does not consume CPU resources, and retains only the running state. So why do some programs cut out to re-enter the main interface. However, if a program wants to process something in the background, such as music playback, it will start a service. The service can continue to run in the background, so there are only applications with services that consume power in the background. This can be seen in the process management software, the tag is the service. As for the radio, I'm not involved. So no application with the service in the background is completely non-power consumption, no need to shut down. This design is a very good design, the next time you start the program will be faster, because there is no need to read the interface resources, why should you turn off the advantages of this android? (Tell us how to use process management software properly)
There is also a reason why Android an app looks so consumed by memory. As you know, Android apps are Java, and of course they need virtual machines, and Android apps have standalone VMs, which means that each application opens a separate virtual machine. The reason for this is that you can avoid a crash of the virtual machine and crash the entire system, but at the cost of more memory. (not the same as Saipan, Android is not easy to crash restart)
These designs ensure the stability of Android and, under normal circumstances, up to a single program crashes, but the whole system does not crash, and there is never a hint of insufficient memory to appear. Everyone may be poisoned by Windows too deep, and always want to keep more memory, but in fact, this does not necessarily increase speed, but the contrary has lost the program to start the system characteristics of the fast, it is not necessary. You might as well use this system as I say.
Good luck to everyone, this system is open for 10 days and half a month is no problem, not windows.

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.