Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何关闭android应用程序?_Android_Forceclose - Fatal编程技术网

如何关闭android应用程序?

如何关闭android应用程序?,android,forceclose,Android,Forceclose,关于这个话题有很多问题,但没有明确的答案。虽然android的内存管理非常可靠,但很多人认为我们不应该杀掉android应用程序。我的情况不同。我想要一个关闭应用程序的选项。我发现下面是关闭应用程序的代码,但有时它不起作用。当我按下应用程序上的退出按钮时,应用程序似乎正在刷新自己 MainActivity.java @Override public void onDestroy() { super.onDestroy(); /*

关于这个话题有很多问题,但没有明确的答案。虽然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);
}
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();
    }
}
FinishActivity.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);
}
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())但它不起作用。

您已使用
系统。退出(0),我建议您不要使用它。使用它不是一种好的编程风格。有一个方法叫做
finish()在活动中完成任何活动的执行。你应该用它


Process.killProcess(Process.myPID())
也不适合使用。

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

Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
为什么要调用一个自身完成的活动(FinishActivity),然后对当前活动(MainActivity)调用finish()——不管怎样,主活动中的finish是没有意义的

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

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

我找到了我的解决方案。使用此选项关闭应用程序

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

除非您有一个非常非常有效的论证,否则您永远不应该以编程方式关闭应用程序。您的可能重复项是正确的,但在“活动”上调用finish()不会完全关闭应用程序。应用程序在任务管理器应用程序中仍然可见。我不想那样。是的,我同意你的看法。但是,恕我直言,Android不能100%保证关闭该活动。@Lucifer:other then system.exit and kill process,是否有任何其他技术可以优雅地完全杀死该应用程序,而不仅仅是一个活动?我将我的应用程序用于一个信息亭,该信息亭不会有专门的用户来管理这些应用程序。作为备份,我需要一个功能来重新启动应用程序(在非常罕见的情况下)。我如何才能做到这一点?@Basher51您找到了解决方案吗?我也在做kiosk应用程序。HomeClass代表什么?HomeClass,代表主屏幕,可以通过按Home键来显示,当设备启动第一个屏幕时。在尝试了许多解决方案后,我发现这是完美的。谢谢you@RaviPatel:这是“杀死”应用程序还是只是在设备上启动默认启动器?@Basher51:它关闭应用程序并启动默认启动器。它不会杀死应用程序,应用程序对象仍然存在,因此应用程序仍然被认为是aliveCode。只有答案没有多大用处。我的代码中有注释,可以解释它。如果有什么不合理的地方,请告诉我:)你在说什么@dergolem?只有代码的答案非常有用@我希望你是在开玩笑吧?只有代码的答案不能解释任何事情。OP不会了解错误是什么以及如何修复。你说这非常有用吗?@dergolem你在说什么?密码不是神秘的魔法废墟。您可以阅读此代码并了解其工作原理。编辑:另外,我要补充的是,代码是一种比英语更不含糊的语言来解释事情。