如何关闭Android应用程序?

时间:2010-01-19 11:07:15

标签: android

我想关闭我的应用程序,以便它不再在后台运行。

怎么做?这是Android平台上的好习惯吗?

如果我依赖“后退”按钮,它会关闭应用程序,但它会保留在后台。甚至有一个名为“TaskKiller”的应用程序只是为了在后台杀死这些应用程序。

22 个答案:

答案 0 :(得分:139)

Android有一种机制可以根据文档安全地关闭应用程序。在退出的最后一个Activity(通常是应用程序启动时首次出现的主Activity)中,只需在onDestroy()方法中放置几行。对 System.runFinalizersOnExit(true)的调用可确保在应用程序退出时完成所有对象并进行垃圾回收。如果您愿意,也可以通过 android.os.Process.killProcess(android.os.Process.myPid())快速终止应用程序。执行此操作的最佳方法是在helper类中放置如下所示的方法,然后在需要杀死应用程序时调用它。例如,在根活动的destroy方法中(假设应用程序永远不会杀死此活动):

Android也不会通知应用程序 HOME 键事件,因此在按下 HOME 键时无法关闭应用程序。 Android会将 HOME 键事件保留给自己,以便开发人员无法阻止用户离开其应用程序。但是,您可以通过在辅助类中将标志设置为true来确定按 HOME 键,该辅助类假定已按下 HOME 键,然后将标志更改为false当一个事件发生时,显示 HOME 键未被按下,然后检查 onStop()方法中按下的 HOME 键。活动。

不要忘记处理任何菜单和菜单启动的活动的 HOME 键。 SEARCH 键也是如此。下面是一些示例类来说明:

以下是在应用程序销毁时杀死应用程序的根活动示例:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 */

public class HomeKey extends CustomActivity {

    public void onDestroy() {
        super.onDestroy();

        /*
         * Kill application when the root activity is killed.
         */
        UIHelper.killApp(true);
    }

}

这是一个抽象活动,可以扩展为处理扩展它的所有活动的 HOME 键:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 */

import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;

/**
 * Activity that includes custom behavior shared across the application. For
 * example, bringing up a menu with the settings icon when the menu button is
 * pressed by the user and then starting the settings activity when the user
 * clicks on the settings icon.
 */
public abstract class CustomActivity extends Activity {
    public void onStart() {
        super.onStart();

        /*
         * Check if the app was just launched. If the app was just launched then
         * assume that the HOME key will be pressed next unless a navigation
         * event by the user or the app occurs. Otherwise the user or the app
         * navigated to this activity so the HOME key was not pressed.
         */

        UIHelper.checkJustLaunced();
    }

    public void finish() {
        /*
         * This can only invoked by the user or the app finishing the activity
         * by navigating from the activity so the HOME key was not pressed.
         */
        UIHelper.homeKeyPressed = false;
        super.finish();
    }

    public void onStop() {
        super.onStop();

        /*
         * Check if the HOME key was pressed. If the HOME key was pressed then
         * the app will be killed. Otherwise the user or the app is navigating
         * away from this activity so assume that the HOME key will be pressed
         * next unless a navigation event by the user or the app occurs.
         */
        UIHelper.checkHomeKeyPressed(true);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings_menu, menu);

        /*
         * Assume that the HOME key will be pressed next unless a navigation
         * event by the user or the app occurs.
         */
        UIHelper.homeKeyPressed = true;

        return true;
    }

    public boolean onSearchRequested() {
        /*
         * Disable the SEARCH key.
         */
        return false;
    }
}

以下是处理 HOME 键的菜单屏幕示例:

/**
 * @author Danny Remington - MacroSolve
 */

package android.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;

/**
 * PreferenceActivity for the settings screen.
 * 
 * @see PreferenceActivity
 * 
 */
public class SettingsScreen extends PreferenceActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.settings_screen);
    }

    public void onStart() {
        super.onStart();

        /*
         * This can only invoked by the user or the app starting the activity by
         * navigating to the activity so the HOME key was not pressed.
         */
        UIHelper.homeKeyPressed = false;
    }

    public void finish() {
        /*
         * This can only invoked by the user or the app finishing the activity
         * by navigating from the activity so the HOME key was not pressed.
         */
        UIHelper.homeKeyPressed = false;
        super.finish();
    }

    public void onStop() {
        super.onStop();

        /*
         * Check if the HOME key was pressed. If the HOME key was pressed then
         * the app will be killed either safely or quickly. Otherwise the user
         * or the app is navigating away from the activity so assume that the
         * HOME key will be pressed next unless a navigation event by the user
         * or the app occurs.
         */
        UIHelper.checkHomeKeyPressed(true);
    }

    public boolean onSearchRequested() {
        /*
         * Disable the SEARCH key.
         */
        return false;
    }

}

这是一个帮助程序类的示例,它在整个应用程序中处理 HOME 键:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 *
 */

/**
 * Helper class to help handling of UI.
 */
public class UIHelper {
    public static boolean homeKeyPressed;
    private static boolean justLaunched = true;

    /**
     * Check if the app was just launched. If the app was just launched then
     * assume that the HOME key will be pressed next unless a navigation event
     * by the user or the app occurs. Otherwise the user or the app navigated to
     * the activity so the HOME key was not pressed.
     */
    public static void checkJustLaunced() {
        if (justLaunched) {
            homeKeyPressed = true;
            justLaunched = false;
        } else {
            homeKeyPressed = false;
        }
    }

    /**
     * Check if the HOME key was pressed. If the HOME key was pressed then the
     * app will be killed either safely or quickly. Otherwise the user or the
     * app is navigating away from the activity so assume that the HOME key will
     * be pressed next unless a navigation event by the user or the app occurs.
     * 
     * @param killSafely
     *            Primitive boolean which indicates whether the app should be
     *            killed safely or quickly when the HOME key is pressed.
     * 
     * @see {@link UIHelper.killApp}
     */
    public static void checkHomeKeyPressed(boolean killSafely) {
        if (homeKeyPressed) {
            killApp(true);
        } else {
            homeKeyPressed = true;
        }
    }

    /**
     * Kill the app either safely or quickly. The app is killed safely by
     * killing the virtual machine that the app runs in after finalizing all
     * {@link Object}s created by the app. The app is killed quickly by abruptly
     * killing the process that the virtual machine that runs the app runs in
     * without finalizing all {@link Object}s created by the app. Whether the
     * app is killed safely or quickly the app will be completely created as a
     * new app in a new virtual machine running in a new process if the user
     * starts the app again.
     * 
     * <P>
     * <B>NOTE:</B> The app will not be killed until all of its threads have
     * closed if it is killed safely.
     * </P>
     * 
     * <P>
     * <B>NOTE:</B> All threads running under the process will be abruptly
     * killed when the app is killed quickly. This can lead to various issues
     * related to threading. For example, if one of those threads was making
     * multiple related changes to the database, then it may have committed some
     * of those changes but not all of those changes when it was abruptly
     * killed.
     * </P>
     * 
     * @param killSafely
     *            Primitive boolean which indicates whether the app should be
     *            killed safely or quickly. If true then the app will be killed
     *            safely. Otherwise it will be killed quickly.
     */
    public static void killApp(boolean killSafely) {
        if (killSafely) {
            /*
             * Notify the system to finalize and collect all objects of the app
             * on exit so that the virtual machine running the app can be killed
             * by the system without causing issues. NOTE: If this is set to
             * true then the virtual machine will not be killed until all of its
             * threads have closed.
             */
            System.runFinalizersOnExit(true);

            /*
             * Force the system to close the app down completely instead of
             * retaining it in the background. The virtual machine that runs the
             * app will be killed. The app will be completely created as a new
             * app in a new virtual machine running in a new process if the user
             * starts the app again.
             */
            System.exit(0);
        } else {
            /*
             * Alternatively the process that runs the virtual machine could be
             * abruptly killed. This is the quickest way to remove the app from
             * the device but it could cause problems since resources will not
             * be finalized first. For example, all threads running under the
             * process will be abruptly killed when the process is abruptly
             * killed. If one of those threads was making multiple related
             * changes to the database, then it may have committed some of those
             * changes but not all of those changes when it was abruptly killed.
             */
            android.os.Process.killProcess(android.os.Process.myPid());
        }

    }
}

答案 1 :(得分:68)

YES!您当然可以关闭应用程序,使其不再在后台运行。与其他人一样,评论finish()是谷歌推荐的方式,并不意味着您的计划已关闭。

System.exit(0);

这就是关闭你的应用程序,不让任何东西在后台运行。但是,明智地使用它,不要让文件打开,数据库句柄打开等等。这些东西通常会通过{{1}清理}命令。

我个人讨厌当我在一个应用程序中选择退出时它并没有真正退出。

答案 2 :(得分:23)

这就是我这样做的方式:

我只是把

Intent intent = new Intent(Main.this, SOMECLASSNAME.class);
Main.this.startActivityForResult(intent, 0);

打开活动的方法内部,然后在SOMECLASSNAME的方法内部,该方法旨在关闭我放置的应用程序:

setResult(0);
finish();

我将以下内容放入我的主课程中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == 0) {
        finish();
    }
}

答案 3 :(得分:18)

在这么多时间之后回答我自己的问题(因为CommonsWare评论了最流行的答案,告诉我们不应该这样做):

当我想退出应用时:

  1. 我使用FLAG_ACTIVITY_CLEAR_TOP启动了我的第一个活动(启动画面,或者当前位于活动堆栈底部的任何活动)(这将取消之后开始的所有其他活动,这意味着 - 所有这些活动)。只是让活动堆栈中有这个活动(不是事先由于某种原因完成它)。
  2. 我在此活动上致电finish()
  3. 这就是它,对我来说效果很好。

答案 4 :(得分:10)

只需在按钮EXIT点击上编写此代码即可。

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("LOGOUT", true);
startActivity(intent);

MainActivity.class onCreate()方法中,将下面的代码写为第一行,

if (getIntent().getBooleanExtra("LOGOUT", false))
{
    finish();
}

答案 5 :(得分:9)

使用框架API是不可能的。操作系统(Android)可以自行决定何时删除进程或保留在内存中。这是出于效率原因:如果用户决定重新启动应用程序,那么它已经存在,而不必将其加载到内存中。

所以不,它不仅气馁,这是不可能的

答案 6 :(得分:8)

退出app方式:

方式1:

致电finish();并覆盖onDestroy();。将以下代码放在onDestroy()

System.runFinalizersOnExit(true)

android.os.Process.killProcess(android.os.Process.myPid());

方式2:

public void quit() {
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    System.exit(0);
}

方式3:

Quit();

protected void Quit() {
    super.finish();
}

方式4:

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

if (getIntent().getBooleanExtra("EXIT", false)) {
     finish();
}

方式5:

有时调用finish()只会退出当前活动,而不是整个应用程序。但是,有一个解决方法。每次启动activity时,请使用startActivityForResult()启动它。如果要关闭整个应用程序,可以执行以下操作:

setResult(RESULT_CLOSE_ALL);
finish();

然后定义每个活动的onActivityResult(...)回调,这样当activity返回RESULT_CLOSE_ALL值时,它也会调用finish()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode){
        case RESULT_CLOSE_ALL:{
            setResult(RESULT_CLOSE_ALL);
            finish();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

答案 7 :(得分:5)

活动上调用finish()方法会对当前活动产生预期效果。

答案 8 :(得分:5)

这就是Windows Mobile的工作方式......好吧......永远!以下是微软就此事所说的话:

http://blogs.msdn.com/windowsmobile/archive/2006/10/05/The-Emperor-Has-No-Close.aspx(从2006年开始我记得博客文章的标题是否很难过?我通过搜索“皇帝没有关闭”在Google上发现这篇文章大声笑)

简而言之:

  

如果系统需要更多内存   应用程序在后台,它会   关闭应用程序。但是,如果系统   应用程序将不需要更多内存   留在RAM并准备好回来   下次用户需要时很快   它

this question at O'Reilly中的许多评论都表明,Android的行为方式大致相同,只有当Android需要他们正在使用的内存时,关闭尚未使用过一段时间的应用程序。

由于这是标准功能,因此将行为更改为强制关闭将改变用户体验。许多用户习惯于轻轻地解雇他们的Android应用程序,因此当他们在执行其他任务后解雇其中一个用户时,他们可能会相当沮丧,因为应用程序的状态被重置,或者需要更长的时间打开。我会坚持标准行为,因为它是预期的。

答案 9 :(得分:3)

以上所有答案都不适合我的应用

这是我的工作代码

退出按钮

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mainIntent.putExtra("close", true);
startActivity(mainIntent);
finish();

该代码将关闭任何其他活动并将MainActivity置于最顶层 现在在您的MainActivity上:

if( getIntent().getBooleanExtra("close", false)){
    finish();
}

答案 10 :(得分:2)

如下所示添加finish();声明:

myIntent.putExtra("key1", editText2.getText().toString());

finish();

LoginActivity.this.startActivity(myIntent);

在每项活动中。

答案 11 :(得分:2)

@Override
    protected void onPause() {

        super.onPause();

        System.exit(0);

    }

答案 12 :(得分:2)

复制下面的代码并将AndroidManifest.xml文件粘贴到First Activity Tag。

<activity                        
            android:name="com.SplashActivity"
            android:clearTaskOnLaunch="true" 
            android:launchMode="singleTask"
            android:excludeFromRecents="true">              
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"
                />
            </intent-filter>
        </activity>     

此外,在AndroidManifest.xml文件

中的活动标记下的所有代码中添加以下代码
 android:finishOnTaskLaunch="true"

答案 13 :(得分:2)

使用“ this.FinishAndRemoveTask();”-它会正确关闭应用程序

答案 14 :(得分:1)

2.3不可能。我搜索了很多,并尝试了很多应用程序。最好的解决方案是同时安装(go taskmanager)和(快速重启)。当它们一起使用时,它将起作用,并释放内存。另一个选择是升级到android冰淇淋三明治4.0.4,它允许控制(关闭)应用程序。

答案 15 :(得分:1)

我想回到我的Android设备的主屏幕, 所以我只是用了:

moveTaskToBack(true);

答案 16 :(得分:1)

如果要关闭应用的所有活动,使用finishAffinity()可能是一个不错的选择。根据Android文档 -

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

答案 17 :(得分:1)

public class CloseAppActivity extends AppCompatActivity
{
    public static final void closeApp(Activity activity)
    {
        Intent intent = new Intent(activity, CloseAppActivity.class);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        finish();
    }
}

并在清单中:

<activity
     android:name=".presenter.activity.CloseAppActivity"
     android:noHistory="true"
     android:clearTaskOnLaunch="true"/>

然后你可以致电CloseAppActivity.closeApp(fromActivity),申请将被关闭。

答案 18 :(得分:1)

只需在onBackPressed中编写以下代码:

@Override
public void onBackPressed() {
    // super.onBackPressed();

    //Creating an alert dialog to logout
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you want to Exit?");
    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    startActivity(intent);
                }
            });

    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                }
            });

    //Showing the alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

答案 19 :(得分:0)

通过调用finish();在OnClick按钮或菜单上

  

案例R.id.menu_settings:

      finish();
     return true;

答案 20 :(得分:0)

我认为它会关闭您的活动以及与之相关的所有子活动。

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();]
        if (id == R.id.Exit) {
            this.finishAffinity();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

答案 21 :(得分:0)

使用表System.exit的最佳和最短的方式。

System.exit(0);

VM停止进一步执行,程序将退出。