如何closuresAndroid应用程序?
我想closures我的应用程序,以便它不再在后台运行。
怎么做? 这是Android平台上的良好做法吗?
如果我依靠“后退”button,它closures应用程序,但它保持在后台。 甚至有一个名为“TaskKiller”的应用程序只是为了在后台杀死这些应用程序。
Android有一个机制来安全地closures每个文档的应用程序。 在退出的最后一个Activity(通常是应用程序启动时首次出现的主Activity)中,只需在onDestroy()方法中放置几行即可。 对System.runFinalizersOnExit(true)的调用可确保在应用程序退出时所有对象都将被最终化并进行垃圾回收。 如果愿意,也可以通过android.os.Process.killProcess(android.os.Process.myPid())快速杀死一个应用程序。 最好的方法是在助手类中放入类似下面的方法,然后在应用程序需要被终止时调用它。 例如,在根活动的销毁方法中(假设应用程序永远不会杀死这个活动):
此外,Android不会通知HOME键事件的应用程序,所以按HOME键时不能closures应用程序。 Android将自己的HOME键事件保留给自己,以便开发人员不能阻止用户离开他们的应用程序。 但是,可以通过在假设已经按下HOME键的助手类中将标志设置为真来确定HOME键被按下,然后当事件发生时将标志改变为假,表示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()); } } }
是! 你可以绝对closures你的应用程序,使它不再在后台运行。 像别人评论finish()
是Google推荐的方式,并不意味着你的程序closures了。
System.exit(0);
那么你的应用程序将closures你的应用程序,而不会在后台运行任何东西。但是,明智地使用它,不要把文件打开,打开数据库句柄等等。这些东西通常会通过finish()
命令来清理。
我个人恨,当我在应用程序中select退出,并没有真正退出。
这是我做的方式:
我只是放
Intent intent = new Intent(Main.this, SOMECLASSNAME.class); Main.this.startActivityForResult(intent, 0);
在打开一个活动的方法里面,然后在SOMECLASSNAME的devise用于closures应用程序的方法里面放上:
setResult(0); finish();
我把下面的内容放在我的Main类中:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == 0) { finish(); } }
只是现在回答我自己的问题了很多时间(因为CommonsWare评论最stream行的答案,告诉我们不应该这样做):
当我想要退出应用程序时:
- 我用
FLAG_ACTIVITY_CLEAR_TOP
开始我的第一个活动(启animation面,或者当前在活动栈底部的任何活动)(它将退出在它之后开始的所有其他活动,这意味着所有这些活动)。 只要在活动堆栈中进行此活动(不提前由于某种原因完成)。 - 我把这个活动叫做
finish()
就是这样,对我来说工作得很好。
使用框架API是不可能的。 操作系统(Android)可以决定何时应该删除或保留在内存中。 这是出于效率原因:如果用户决定重新启动应用程序,那么它已经在那里,而不必将其加载到内存中。
所以不, 不但不鼓励,而且也不可能这样做。
只需在您的buttonEXIT点击这个代码。
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(); }
对于退出应用程序的方式:
方式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()
来启动它。 当你想closures整个应用程序,你可以做如下的事情:
setResult(RESULT_CLOSE_ALL); finish();
然后定义每个活动的onActivityResult(...)
callback,所以当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); }
在Activity上调用finish()
方法会对当前活动产生期望的效果。
这是Windows Mobile如何工作…以及…永远! 这就是微软在这个问题上所要说的:
http://blogs.msdn.com/windowsmobile/archive/2006/10/05/The-Emperor-Has-No-Close.aspx (难道我记得2006年以来的博客文章的标题?我发现在谷歌search“皇帝没有closures”的文章大声笑)
简而言之:
如果系统需要更多的内存,而应用程序在后台,它会closures应用程序。 但是,如果系统不需要更多的内存,应用程序将保留在RAM中,并准备在用户下一次需要时快速回来。
O'Reilly在这个问题上的许多评论都表明,Android的行为方式大致相同,只有在Android需要使用内存的情况下,才能closures那些尚未使用的应用程序。
由于这是标准function,因此改变行为强制closures将改变用户体验。 很多用户会习惯于温和地解雇他们的Android应用,所以当他们在执行一些其他任务后想要返回时,他们可能会感到沮丧,因为应用的状态被重置,或者需要更长的时间打开。 我会坚持标准的行为,因为这是预期的。
以上所有答案都没有在我的应用程序工作良好
这是我的工作代码
在退出button上:
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();
该代码将closures其他任何活动,并将MainActivity放在MainActivity上:
if( getIntent().getBooleanExtra("close", false)){ finish(); }
放finish();
声明如下:
myIntent.putExtra("key1", editText2.getText().toString()); finish(); LoginActivity.this.startActivity(myIntent);
在每一个活动。
@Override protected void onPause() { super.onPause(); System.exit(0); }
2.3不可能。 我search很多,并尝试了很多应用程序。 最好的解决办法是同时安装(去任务pipe理器)和(快速重启)。 当它们一起使用时,它将起作用,并释放内存。 另一个select是升级到Android冰淇淋三明治4.0.4,允许控制(closures)的应用程序。
复制以下代码并将AndroidManifest.xml文件粘贴到第一个活动标签下。
<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文件的Activity Tag下添加下面的代码
android:finishOnTaskLaunch="true"
我想回到我的Android设备的主屏幕,所以我只是使用:
moveTaskToBack(true);
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)
,应用程序将被closures。
只需在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(); }
通过调用finish(); 在OnClickbutton或菜单上
案例R.id.menu_settings:
finish(); return true;
如果要closures应用程序的所有Activity,那么使用finishAffinity()
可能是一个不错的select。 按照Android Docs-
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
我认为这将closures你的活动和所有相关的子活动。
public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId();] if (id == R.id.Exit) { this.finishAffinity(); return true; } return super.onOptionsItemSelected(item); }
使用表System.exit的最好和最短的方法。
System.exit(0);
虚拟机停止进一步执行,程序将退出。
- Android Studio SDKpipe理已禁用
- 执行失败的任务':app:compileDebugAidl':aidl缺失
- 错误:无法在Android Studio中定位SDK中的adb
- 如何杀死一个Android的活动,当离开它,以便它不能从后退button访问?
- 无法实例化android.gms.maps.MapFragment
- 如何解除Android中的系统对话框?
- 什么原因导致Android的ContentResolver.query()返回null?
- 如何在Android Studio中获取SHA-1指纹证书的debugging模式?
- 在Android Studio中导入Facebook库:无法find属性“ANDROID_BUILD_SDK_VERSION”