继续学习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,但它现在看起来不太好=(

有办法让我真的退出应用程序吗?


当前回答

我同意泰德的观点。我明白退出应用程序不是 “Android方式”,但它似乎不应该被排除。在这里 您可能需要一个真正的应用程序出口(不是 只是活动):

The user might want some control over which app gets killed in the case of low memory. If important app A is running in the background, then you might like to exit app B when you are done with it so that app A doesn't get killed by the operating system. If your application has sensitive data cached in memory, you might like to kill the app so that a virus/worm/rogue app can't get at it. I know the security model is supposed to prevent that, but just in case... If your application uses resources (like network, CPU, sensors, etc.) that could adversely affect the phone, then one way of ensuring that those resources are freed up is to exit the application. I understand that well-behaved apps should free up resources when they are not needed. But again, exiting the application seems like a reasonable way of ensuring that.

其他回答

泰德,你想要实现的目标是可以实现的,只是可能不是你现在想的那样。

我建议您阅读活动和服务。停止使用术语“应用程序”,开始引用组件,即活动,服务。我认为你只需要更多地了解Android平台;这是一个标准PC应用程序的心态变化。事实上,你的帖子中没有一个词是“活动”(简单的FAQ引用,即不是你的话),我告诉我你需要阅读更多。

这场争论可以归结为一个古老的问题:究竟是开发人员最了解,还是用户最了解。所有人为因素领域的专业设计师每天都在与这个问题作斗争。

Ted指出,市场上下载最多的应用之一是“应用杀手”。当人们停止应用时,他们会获得额外的血清素。他们已经习惯了台式机/笔记本电脑。它能让事情快速发展。它能让处理器保持凉爽,风扇也不会打开。它耗电更少。

当你认为移动设备是一艘小得多的船时,你就会特别感激他们“抛弃你不再需要的东西”的动机。现在,Android的开发者们认为操作系统才是最清楚的,退出一个应用程序已经过时了。我全心全意地支持这一点。

然而,我也认为你不应该让用户感到沮丧,即使这种沮丧是出于他们自己的无知。因此,我认为有一个“退出”选项是很好的设计,即使它只是一个安慰剂按钮,只是关闭一个视图。

如果你有10个,20个…有多个活动正在运行,你想要完成所有活动并退出系统。

在应用程序类或常量类中创建静态数组。

常量

public class Constants {

public static ArrayList<Activity> activities = new ArrayList<Activity>();

}

在此数组中添加当前活动引用

activity = MainActivity.this; Constants.activities.add(活动);

public class MainActivity extends Activity {

    private ImageView imageButton;
    private Activity activity;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        activity = MainActivity.this;
        Constants.activities.add(activity);

        imageButton = (ImageView) findViewById(R.id.camera);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // existing app.
                if (Constants.activities != null) {
                    for (int i = 0; i < Constants.activities.size(); i++) {
                        Activity s = Constants.activities.get(i);
                        s.finish();
                    }
                }
                //super.finish();
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }
        });
    }
}

显然,您已经在finish()命令中找到了想要的答案。这不会将你的应用从内存中移除,但Android会在它需要资源的时候这样做,所以你不会显式地这样做没有任何区别。

我只想补充一点,为了达到应用程序退出通常具有的全部效果,您需要将应用程序的状态重置为设备引导后第一次运行时的正常状态,就在对所有活动调用finish()之前。这样,如果用户再次选择你的应用程序,它将显示为“新鲜”运行,在模拟“退出”之前没有任何状态。

如果有一些特殊的操作应该只在“退出”时发生,比如保存用户的工作或其他什么,您也可以在上述例程的重新初始化部分之前执行它们。

这种方法可以让你实现“退出”命令的目标,而不违反Android的理念,即将操作系统资源的管理(包括关闭应用程序)交给操作系统。

就我个人而言,我不会使用这种方法,因为Android用户希望应用程序在他们重新访问时保持其连续性,因此他们不习惯“退出”应用程序的模式。相反,我支持“清除”函数,用户可以调用该函数将应用程序重置为某个默认的初始状态,而无需在此过程中“离开”它。

唯一的例外是,当用户按下后退按钮的次数足够多,导致应用程序关闭。在这种情况下,用户不希望状态被保存(如果应用程序中有未保存的状态,那么作为开发人员,你应该有代码处理返回按钮,检测未保存的数据,并提示用户将其保存到SharedPreferences或文件,或其他一些非易失性介质)。

关于system . exit (0):

如果你决定使用system.exit(0)关闭你的应用程序与粗鲁的终结(例如,作为一个最后的后退按钮按下的结果),那么我要警告你,虽然对我来说这是“工作”,在某些情况下,我已经能够关闭一个应用程序没有任何痕迹,有一个小故障发生在果冻豆当你使用这种方法。

具体来说,如果你使用最近的应用程序列表打开你的应用程序,然后使用返回按钮关闭应用程序(通过system.exit(0)实现关闭),最近的应用程序列表将再次可见,因为它永远不会被关闭。如果你在该列表中点击应用程序的条目,从相同的、已经打开的、最近的应用程序列表中再次运行它,将没有响应。

我怀疑造成这种情况的原因是,最近的应用程序列表持有一个对你的应用程序的引用,由于你已经使用system.exit(0)关闭了应用程序,该应用程序已变得无功能。使用finish()更文明地关闭应用程序可能会通知操作系统,允许它刷新其最近应用程序列表,但system.exit(0)显然没有这样做。

This is not a huge problem in and of itself, as very few people will open an app from Recent Apps, then exit it, and then immediately open it again from the same open Recent Apps list. And if they tap the home button and then re-open the Recent Apps list, your app's entry will be there, and it will be fully functional. But I think that it shows that the use of system.exit(0) can interfere with proper communication between your app and the OS, and this suggests that there may be other, more serious, possibly subtle, consequences of using this approach.

每次当你通过意图移动到下一页时,使用:

`YourActivityname.this.finish()`;

例子:

Intent intent = new Intent(getApplicationContext(), SMS.class);

startActivity(intent);
MainActivity.this.finish();

因此,没有活动将在后台运行,当你想退出你的应用程序,使用:

MainActivity.this.finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
getParent().finish();

这种退出对我来说就像一个魅力:)