如何关闭Android应用程序? [英] How to close an android application?

查看:159
本文介绍了如何关闭Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关于该主题的许多问题,但目前还没有明确的答案。虽然Android的内存管理是非常稳固,所以很多人认为,我们不应该杀了android应用。我的情况是不同的。我想一个选项,关闭应用程序。我发现下面的$ C $下关闭应用程序,但有时不work.It似乎应用程序只是刷新自己,当我打我的应用程序退出按钮。

There are many question regarding this topic but there is no clear answer. Although android's memory management is very solid, so many people believe that we shouldn't kill android application. My case is different. I want an option to close an application. I found following code for closing application but sometimes it doesn't work.It seems application just refreshing itself when i hit exit button on my application.

MainActivity.java

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

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()); ,但它不能正常工作

I also tried Process.killProcess(Process.myPid()); but it doesn't work.

推荐答案

我发现我的解决方案。使用它可以关闭应用程序

I found my solution. Use this to close an application

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

这篇关于如何关闭Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆