继续学习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,但它现在看起来不太好=(
有办法让我真的退出应用程序吗?
对于应用程序的第一个(启动)活动,
@Override
public void onBackPressed(){
// Exit
moveTaskToBack(true);
}
为我工作。我想在这里关闭应用程序。从其他活动中回来;我用了意图,例如:
@Override
public void onBackPressed(){
// Going back....
Intent intent = new Intent(ActivityB.this, ActivityA.class);
startActivity(intent);
finish();
}
注意:这段代码适用于开发人员想要从ActivityZ返回ActivityA然后关闭应用程序的场景。
不要把您的应用程序看作一个整体应用程序。它是一组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:
Intent i1 = new Intent(this, AnotherActivity);
startActivity(i1)
你只需要添加finish();例如,从头到尾在每个活动上启动intent活动后,
Intent i1=new Intent(this, AnotherActivity);
startActivity(i1)
finish();
所以当你点击退出按钮时使用的是finish()或System.exit(0)必须完全关闭你的应用程序。