gpt4 book ai didi

android - 如何关闭安卓应用程序?

转载 作者:IT老高 更新时间:2023-10-28 12:55:54 27 4
gpt4 key购买 nike

我想关闭我的应用程序,使其不再在后台运行。

如何做到这一点?这是 Android 平台上的良好做法吗?

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

最佳答案

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

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

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

下面是一个根 Activity 的示例,它在应用程序被销毁时将其杀死:

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

}

这是一个可以扩展的抽象 Activity ,以处理扩展它的所有 Activity 的 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());
}

}
}

关于android - 如何关闭安卓应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2092951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com