如何关闭安卓应用程序?

14

关于这个主题有很多问题,但没有明确的答案。虽然安卓的内存管理非常稳定,所以许多人认为我们不应该关闭安卓应用程序。但我的情况不同,我想要一个关闭应用程序的选项。我找到了下面的代码来关闭应用程序,但有时它不起作用。当我点击应用程序上的退出按钮时,似乎应用程序只是在刷新自身。

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()); 但它不起作用。


1
除非您有非常、非常、非常充分的论据,否则永远不应该通过编程方式关闭应用程序。 - marzapower
可能是如何在程序中强制停止我的Android应用?的重复问题。 - Shirish Herwade
6个回答

34

我找到了解决方案。使用此代码关闭应用程序

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

经过尝试了许多解决方案,我发现这个是完美的..谢谢。 - Mitul Gedeeya
@RaviPatel:这个操作是关闭一个应用程序还是只是启动设备上的默认启动器? - Basher51
@Basher51:它关闭应用程序并启动默认的启动器。 - Ravi Patel
3
不会杀死应用程序,应用程序对象仍然存在,因此应用程序仍被视为处于活动状态。 - NikkyD

10
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();
    }
}

1
我在代码中有注释来解释它。如果有什么不清楚的地方,请告诉我 :) - Cameron Clough
3
你在说什么,@dergolem?只提供代码的答案非常有用!请说明白。 - swooby
1
@swooby 你是在开玩笑吗?仅有代码的答案并不能解释任何东西。提问者不会知道错误出在哪里以及如何修复它。你认为这样非常有用吗? - Phantômaxx
FYI,相关文档链接:finish()finishAndRemoveTask() - Michael Osofsky
解决方案似乎无法完全关闭应用程序。当我“重新打开”应用程序时,它仍然保留了一些之前关闭时的状态。 - Michael Osofsky
显示剩余4条评论

4
你曾使用过 System.exit(0);,我建议你不要使用它。这不是一个好的编程风格。Activity中有一个叫做finish();的方法来结束任何Activity的执行。你应该使用它。
同样,Process.killProcess(Process.myPID());也不是首选方法。

1
你说得没错,但是在activity上调用finish()并不能完全关闭应用程序。 应用程序仍然可在任务管理器应用程序中看到。 我不想发生这种情况。 - Ravi Patel
同意你的观点。但据我所知,Android不能保证完全关闭Activity。 - Lucifer
@Lucifer:除了system.exit和kill process之外,还有其他技术可以优雅地完全关闭应用程序而不仅仅是一个活动吗?我正在为一个信息亭使用我的应用程序,它将没有专门的用户来管理应用程序。作为备份,我需要一种功能,在极少数情况下可以自行启动应用程序。我该怎么做? - Basher51
@Basher51,你找到解决方法了吗?我也在做自助服务应用程序。 - Sam

1
如果你想退出一个活动,请使用活动的finish()方法,就像Lucifer建议的那样。它将简单地结束当前活动。但是如果你想退出应用程序(销毁所有活动直到主屏幕),请使用以下代码块:
Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

HomeClass代表什么? - Ravi Patel
HomeClass代表主屏幕,可以通过按Home键显示,并且在设备启动时作为第一个屏幕显示。 - jeet

1
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() - 无论如何,在主活动中的finish都是毫无意义的。

0
@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);
        }
    }

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接