继续学习Android,我读了以下内容:
Question: Does the user have a choice to kill the application
unless we put a menu option in to kill it? If no such option exists,
how does the user terminate the application?
Answer: (Romain Guy): The user doesn't, the system handles this automatically. That's what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a "quit" or "exit" application button. It is useless with Android's application model. This is also contrary to how core applications work.
呵呵,我在Android世界里每走一步都会遇到一些问题=(
显然,你不能在Android中退出应用程序(但Android系统可以随时完全销毁你的应用程序)。这是怎么回事?我开始认为这是不可能写一个应用程序的功能作为“正常的应用程序”-用户可以退出应用程序时,他/她决定这么做。这不应该依靠操作系统来完成。
我正在尝试创建的应用程序不是Android市场的应用程序。它不是一个被大众“广泛使用”的应用程序,它是一个将在非常狭窄的商业领域使用的商业应用程序。
我其实很期待为Android平台开发,因为它解决了Windows Mobile和。net中存在的许多问题。然而,上周对我来说有点令人失望……我希望我不必放弃Android,但它现在看起来不太好=(
有办法让我真的退出应用程序吗?
我会考虑阅读Addison-Wesley出版的“Android Wireless Application Development”。我刚刚完成它,它是非常彻底的。
看来你对Android平台有一些基本的误解。一开始我也对Android应用程序的生命周期感到有点沮丧,但在更深入地了解后,我开始真正享受这种方法。这本书将回答你所有的问题,甚至更多。这确实是我为Android新开发者找到的最好的资源。
Also, I think you need to let go of a line-for-line port of the existing app. In order to port your application to the Android platform, some of the application design is going to change. The application-lifecycle used is necessary as mobile devices have very limited resources relative to desktop systems and allows Android devices to run several applications in an orderly and resource-aware fashion. Do some more in depth study of the platform, and I think you will realize that what you are wanting to do is entirely feasible. Best of luck.
顺便说一下,我与艾迪生-卫斯理或与这本书有关的任何个人或组织都没有任何关系。重读了我的帖子后,我觉得我有点像个迷弟。我真的非常非常喜欢它,而且发现它非常有帮助。:)
我花了更长的时间来阅读这个问答,而不是真正实现一个半正确的Android应用程序生命周期。
这是一个GPS应用程序,轮询积分,并每隔几秒钟通过线程将当前位置发送到web服务…在Ted的情况下,这可能是每5分钟轮询一次更新,然后onStop可以简单地启动Ted所关心的更新活动,如果发现了一个(异步Ted,不要像Windows程序员那样编码,否则您的程序将像Windows程序一样运行……呃,这也没那么难)。
我在onCreate中做了一些初始代码来设置活动生命周期,包括checkUpdate.start();:
...
@Override
public void onStart() {
super.onStart();
isRemote = true;
checkUpdate.resume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, luh);
}
@Override
public void onPause() {
isRemote = false;
checkUpdate.suspend();
locationManager.removeUpdates(luh);
super.onStop();
}
这段代码可能是完全错误的,但它是有效的。这是我的第一个Android应用程序。
Voilà, an application that doesn't consume CPU when it's in the background, yet is instantly ready to reopen because it is in RAM (although not holding RAM as is the Android lifecycle) ... an app is always ready, it's a phone, guys/gals. If an app was to use up all the RAM and couldn't be shut down by the OS then the thing might stop ringing =P That's why the OS needs to be able to close your app when it's in the background (if your application isn't a resource hog it won't be closed BTW), so let's just write better applications.
博客文章“何时在Android应用程序中包含退出按钮”(提示:永远不要)解释得比我好得多。我希望每个Android开发者都读过这本书。
摘录:
In my experience what [the users] really want is:
An unambiguous way to guarantee that an app will stop consuming resources (battery, CPU cycles, data transfer, etc.).
Many users perceive that an exit button implements this requirement
and ask for it to be added. Developers, looking to please their users,
obligingly add one. Shortly thereafter they both fail.
In most cases the exit button simply calls Activity.finish(). This is exactly equivalent to hitting the back button.
Exactly. Services keep running and polling keeps happening. Users may think they've killed the app but they haven't, and soon
they'll be even more annoyed.
Exit behavior is now ambiguous. Should your exit button just close the Activity, or should it also stop all associated Services, Receivers, and Alarms? What should Back do? What happens if they hit Home instead? What happens if your app has a widget? Should the exit button stop that from updating too?
The solution is to make the back button behave as you'd expect the
exit button to. Better yet, simply stop consuming resources whenever
the app isn't visible.
继续阅读完整的文章。
不要把您的应用程序看作一个整体应用程序。它是一组UI屏幕,用户可以通过Android服务与你的“应用程序”和“功能”进行交互。
不知道你的神秘应用程序“做什么”并不重要。让我们假设它通过隧道进入某个超级安全的企业内部网,执行一些监控或交互,并保持登录状态,直到用户“退出应用程序”。因为您的IT部门命令它,所以用户必须非常清楚他们何时进入或退出内部网。因此你认为用户“退出”很重要。
这很简单。创建一个服务,在通知栏中放置一个持续通知,说“我在内部网中,或者我正在运行”。让该服务执行应用程序所需的所有功能。拥有绑定到该服务的活动,以允许用户访问与“应用程序”交互所需的UI部分。并有一个Android菜单->退出(或登出,或其他)按钮,告诉服务退出,然后关闭活动本身。
This is, for all intents and purposes exactly what you say you want. Done the Android way. Look at Google Talk or Google Maps Navigation for examples of this "exit" is possible mentality. The only difference is that pressing back button out of your activity might leave your UNIX process lying in wait just in case the user wants to revive your application. This is really no different than a modern operating system that caches recently accessed files in memory. After you quit your windows program, most likely resources that it needed are still in memory, waiting to be replaced by other resources as they are loaded now that they are no longer needed. Android is the same thing.
我真不明白你有什么问题。
在Intent中使用FLAG_ACTIVITY_CLEAR_TOP标志关闭应用程序,然后system.exit();
或者有类似的方法,但是没有system.exit()当你想退出时调用这个方法:
public void exit() {
startActivity(new Intent(this, HomeActivity.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK).putExtra(EXIT_FLAG, true));
}
在你的HomeActivity.onCreate()中添加以下代码
protected void onCreate(Bundle savedInstanceState) {
if (getIntent().getBooleanExtra(EXIT_FLAG, false)) {
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
finish();
}
}
......................
这将在不破坏Android生命周期的情况下工作。