【问题标题】:How to close an android application?如何关闭安卓应用程序?
【发布时间】:2012-04-16 00:24:07
【问题描述】:

关于这个话题有很多问题,但没有明确的答案。虽然android的内存管理很扎实,但是很多人认为我们不应该杀掉android应用。我的情况不同。我想要一个关闭应用程序的选项。我找到了以下用于关闭应用程序的代码,但有时它不起作用。当我点击应用程序上的退出按钮时,应用程序似乎只是在自我刷新。

MainActivity.java

@Override
    public void onDestroy()
    {
        super.onDestroy();
        /*
         * Notify the system to finalize and collect all objects of the
         * application on exit so that the process running the application can
         * be killed by the system without causing issues. NOTE: If this is set
         * to true then the process will not be killed until all of its threads
         * have closed.
         */
        System.runFinalizersOnExit(true);

        /*
        * Force the system to close the application down completely instead of
        * retaining it in the background. The process that runs the application
        * will be killed. The application will be completely created as a new
        * application in a new process if the user starts the application
        * again.
        */
        System.exit(0);
    }

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
   case R.id.close:
                Intent intentFinish = new Intent(this, FinishActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intentFinish);
                finish();
                return true;
}
return super.onMenuItemSelected(featureId, item);
}

FinishActivity.java

package com.mypackage;

import android.app.Activity;
import android.os.Bundle;

public class FinishActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        finish();
    }
}

我也尝试了Process.killProcess(Process.myPid());,但它不起作用。

【问题讨论】:

标签: android forceclose


【解决方案1】:

我找到了我的解决方案。使用它来关闭应用程序

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);

【讨论】:

  • 在尝试了很多解决方案后,我发现这是完美的..谢谢
  • @RaviPatel:这会“杀死”应用程序还是只是启动设备上的默认启动器?
  • @Basher51:它关闭应用程序并启动默认启动器。
  • 它不会杀死应用程序,应用程序对象仍然存在,因此应用程序仍然被认为是活动的
【解决方案2】:
public void endTask() {
    // Is the user running Lollipop or above?
    if (Build.VERSION.SDK_INT >= 21) { 
        // If yes, run the fancy new function to end the app and
        //  remove it from the task list.
        finishAndRemoveTask();
    } else {
        // If not, then just end the app without removing it from
        //  the task list.
        finish();
    }
}

【讨论】:

  • 纯代码答案没有多大用处。
  • 我的代码中有 cmets 应该解释它。如果有什么不明白的地方请告诉我:)
  • 你在说@dergolem吗?纯代码答案非常很有用!
  • @swooby 你在开玩笑吧,我希望?仅代码的答案不能解释任何事情。 OP 不会了解 WHAT 错误是什么以及 HOW 它是如何修复的。你说这非常有用
  • @dergolem 你在说什么?密码不是神秘的魔法废墟。你可以阅读这段代码,看看它是如何工作的。编辑:进一步我要添加代码是比英语更不模糊的语言来解释事情。
【解决方案3】:

你用过System.exit(0);,建议你不要用。使用它不是一种好的编程风格。 Activity中有一个叫做finish();的方法可以完成任何Activity的Execution。你应该使用它。

Process.killProcess(Process.myPID()); 也不宜使用。

【讨论】:

  • 你是对的,但在活动上调用 finish() 并不能完全关闭应用程序。应用程序在任务管理器应用程序中仍然可见。我不希望这种情况发生。
  • 是的,同意你的看法。但是 AFIAIK,Android 并不能 100% 保证关闭 Activity。
  • @Lucifer:除了 system.exit 和终止进程,还有其他技术可以优雅地完全终止应用程序而不仅仅是一个活动吗?我正在将我的应用程序用于信息亭,它没有专门的用户来处理这些应用程序。作为备份,我需要一个功能来重新启动应用程序(在非常罕见的情况下)它自己。我该怎么做这个?
  • @Basher51 你找到解决方案了吗?我也在做信息亭应用程序。
【解决方案4】:

如果您想退出活动,请使用活动的 finish() 方法,正如 Lucifer 所建议的那样。它将简单地完成当前活动。但是,如果您想退出应用程序(销毁所有活动到主屏幕),请使用以下代码块:

Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

【讨论】:

  • HomeClass 代表什么?
  • HomeClass,代表Home Screen,可以通过HomeKey显示,当设备启动第一个Screen时。
【解决方案5】:
switch(item.getItemId()) {
case R.id.close:
            Intent intentFinish = new Intent(this,FinishActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intentFinish);
            finish();
            return true;
}

你为什么要调用一个自己完成的活动 (FinishActivity),然后在当前活动 (MainActivity) 上调用 finish() - 不管怎样,主活动中的完成是没有意义的。

【讨论】:

    【解决方案6】:
    @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(keyCode == KeyEvent.KEYCODE_BACK)
            {
                finish();
                return true;
            }
            else{
                return super.onKeyUp(keyCode, event);
            }
        }
    

    在应用启动时启动的第一个活动中使用上述方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      相关资源
      最近更新 更多