android 退出应用方法总结
2012-11-11 15:52
225 查看
在android中,当app有多个页面,并且跳转到新页面,当前页面还不能关闭时,就需要对整个应用的activity进行管理,以便于在退出应用时能关闭掉所有的Activity。
baidu、google后发现了下面方法:
activityManager.restartPackage(application.getpackagename());
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
但是,这些方法都不能有效的终结掉app。没办法,只能曲线救国,找了些前辈们总结的办法,测试一番后、总结如下:
1.跳转到HOME页面
测试之后发现,这种方法并不能退出应用,当应用中有没有关闭的页面时,再次点击进入应用后,显示的是没有关闭的页面。
2.写一个工具类统一管理所有的Activity:(在网上扒了篇不错的代码),(类中的 static成员 activityStack 会不会在系统资源不足时被回收???)
3.利用android管理Activity栈的方法:应用的首页HomeActivity,设置启动模式lauchMode="singleTop",在跳转到其他页面后,HomeActivity将会保存在Activity栈底,退出应用的方法:
4.利用广播机制退出
写一个父类ParentActivity,其他所有的activity不再继承Activity而直接继承ParentActivity
2、3、4三种方法都能实现关闭应用的目的,但是,在点开应用开启数个页面,点击home返回home页面,开启一堆其他程序后,在系统干掉我的应用后,再次点开应用,调用退出方法时,这些办法会失效。
5.boolean标志退出
6.制造崩溃异常退出 (参照http://www.2cto.com/kf/201207/138563.html)
自定义Application类继承Application 并实现Thread.UncaughtExceptionHandler接口
将MyApplication配置到Mainfest中
测试环境:m1s,android4.0
baidu、google后发现了下面方法:
activityManager.restartPackage(application.getpackagename());
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
但是,这些方法都不能有效的终结掉app。没办法,只能曲线救国,找了些前辈们总结的办法,测试一番后、总结如下:
1.跳转到HOME页面
public void exitApp() { Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent); finish(); }
测试之后发现,这种方法并不能退出应用,当应用中有没有关闭的页面时,再次点击进入应用后,显示的是没有关闭的页面。
2.写一个工具类统一管理所有的Activity:(在网上扒了篇不错的代码),(类中的 static成员 activityStack 会不会在系统资源不足时被回收???)
import java.util.Stack; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; /** * <b>Android_完全退出</b><br> * <br> * 添加权限 uses-permission android:name="android.permission.RESTART_PACKAGES"<br> * <br> * AppManager类是一个自定义的工具类,作用是将应用程序所有启动的Activity都添加到堆栈,最终退出应用程序时全部释放掉Activity。 <br> * <br> * 使用AppManager工具类 Activity启动时,<br> * 在onCreate方法里面,将该Activity实例添加到AppManager的堆栈 * <b>AppManager.getAppManager().addActivity(this)</b>; <br> * 需要退出程序时,调用<b> AppManager.getAppManager().AppExit(this)</b>; * */ public class AppManager { private static Stack<Activity> activityStack; private static AppManager instance; private AppManager() { } /** * 单一实例 */ public static AppManager getAppManager() { if (instance == null) { instance = new AppManager(); } return instance; } /** * 添加 Activity 到堆栈 * * @param activity */ public void addActivity(Activity activity) { if (activityStack == null) { activityStack = new Stack<Activity>(); } activityStack.add(activity); } /** * 获取当前Activity(堆栈中最后一个压入的) * * @return */ public Activity currentActivity() { Activity activity = activityStack.lastElement(); return activity; } /** * 结束当前Activity (堆栈中最后一个压入的) */ public void finishActivity() { Activity activity = activityStack.lastElement(); if (activity != null) { activity.finish(); activity = null; } } /** * 结束指定的Activity * * @param activity */ public void finishActivity(Activity activity) { if (activity != null) { activityStack.remove(activity); activity.finish(); activity = null; } } /** * 结束指定类名的Activity * * @param cls */ public void finishActivity(Class<?> cls) { for (Activity activity : activityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); } } } /** * 结束所有Activity */ public void finishAllActivity() { for (int i = 0, size = activityStack.size(); i < size; i++) { Activity activity = activityStack.get(i); if (null != activity) { activity.finish(); } } activityStack.clear(); } /** * 退出应用程序 * * @param context */ public void AppExit(Context context) { try { finishAllActivity(); ActivityManager activityMgr = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); activityMgr.restartPackage(context.getPackageName()); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } }
3.利用android管理Activity栈的方法:应用的首页HomeActivity,设置启动模式lauchMode="singleTop",在跳转到其他页面后,HomeActivity将会保存在Activity栈底,退出应用的方法:
public void exitApp() { Intent intent = new Intent(CurrentActivity.this, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 在跳转到处于栈底的HomeActivity页面时,会将处于栈中所有在HomeActivity上的Activity全部出栈,最后再关闭掉Homeactivity即可 startActivity(intent); }
4.利用广播机制退出
写一个父类ParentActivity,其他所有的activity不再继承Activity而直接继承ParentActivity
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; public class ParentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter("com.app.exit"); registerReceiver(exitReceiver, filter);// 注册broadcastreceiver } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(exitReceiver);// 此处一定要 取消broadcastreceiver } public void exitApp() { sendBroadcast(new Intent("com.app.exit")); } private BroadcastReceiver exitReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { finish(); } }; }
2、3、4三种方法都能实现关闭应用的目的,但是,在点开应用开启数个页面,点击home返回home页面,开启一堆其他程序后,在系统干掉我的应用后,再次点开应用,调用退出方法时,这些办法会失效。
5.boolean标志退出
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; public class ParentActivity extends Activity { private static boolean isQuit = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void onResume() { super.onResume(); isQuit = false; } @Override protected void onRestart() { if (isQuit) { finish(); } super.onRestart(); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { if (isQuit) { finish(); } super.onRestoreInstanceState(savedInstanceState); } public void exitApp() { isQuit = true; finish(); } }
6.制造崩溃异常退出 (参照http://www.2cto.com/kf/201207/138563.html)
自定义Application类继承Application 并实现Thread.UncaughtExceptionHandler接口
package com.own.test; import android.app.Application; import android.os.Looper; import android.widget.Toast; public class MyApplication extends Application implements Thread.UncaughtExceptionHandler{ Thread.UncaughtExceptionHandler handler; @Override public void onCreate() { super.onCreate(); // 获取系统默认处理崩溃异常handler //handler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); } @Override public void uncaughtException(Thread thread, Throwable ex) { new Thread(){ public void run() { Looper.prepare(); Toast.makeText(getApplicationContext(), "程序退出", Toast.LENGTH_LONG).show(); Looper.loop(); }; }.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
将MyApplication配置到Mainfest中
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name=".MyApplication"> </application>
测试环境:m1s,android4.0
相关文章推荐
- Android退出应用方法总结
- Android Back键点击两次退出应用详解及实现方法总结
- android开发游记:关闭所有Activity安全退出应用的方法总结
- Android应用开发完全退出程序的通用方法!!!
- Android退出整个应用的方法
- Android应用开发完全退出程序的通用方法!!!
- Android 获取前台应用的方法总结
- Android 系统应用调用,intent 的使用方法总结
- Android 完全退出应用方法
- android 应用常见退出方法
- Android 应用退出的几种方法
- Android应用中去掉标题栏方法总结
- Android开发中一种两次点击返回键退出应用的方法
- android应用完全退出的方法(续)
- Android 应用层面屏蔽状态栏方法总结
- android应用完全退出的方法
- Android 双击返回键退出程序的方法总结
- Android退出应用程序的方法总结
- Android 启动 Activity和一键退出应用的的最佳方法
- android 完全退出应用总结