Android Activity回收与操作超时处理
程序员文章站
2024-02-29 12:27:22
本文实例为大家分享了android activity回收与操作超时的处理,供大家参考,具体内容如下
1、activity的回收
针对多个activity退出的处理
关...
本文实例为大家分享了android activity回收与操作超时的处理,供大家参考,具体内容如下
1、activity的回收
针对多个activity退出的处理
关键代码:
1)、新建活动管理类:
public class activitycollector { private static list<activity> activitylist = new arraylist<activity>(); public static void addactivity(activity activity){ activitylist.add(activity); } public static void removeactivity(activity activity){ activitylist.remove(activity); } public static void finishallbutlast(){ activity activity = activitylist.get(activitylist.size()-1); removeactivity(activity); for (activity activityitem: activitylist){ if (!activityitem.isfinishing()){ activityitem.finish(); } } activitylist.clear(); activitylist.add(activity); } public static void finishall(){ for (activity activity: activitylist){ if (!activity.isfinishing()){ activity.finish(); } } activitylist.clear(); } }
2)、创建基类baseactivity,并使所有的activity继承自该基类 。在创建时,添加到活动管理器,销毁时,从活动管理器中移除。
public class baseactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); activitycollector.addactivity(this); } @override protected void ondestroy() { super.ondestroy(); activitycollector.removeactivity(this); } }
如果需要销毁所有activity,只需调用finishall()即可
2、操作超时处理
原理:
1)、在activity的stop函数中,根据app进程importance_foreground判断app在前台或后台
2)、在activity的onresume函数中,做超时检查。
关键代码:
abstract public class timeoutcheckactivity extends baseactivity { private boolean isleave = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); pref = getsharedpreferences(constant.config_name, context.mode_private); } /** * 回调函数,方便测试 * @return */ abstract protected string gettag(); ......省略号...... /*** * 当用户使程序恢复为前台显示时执行onresume()方法,在其中判断是否超时. */ @override protected void onresume() { // log.i("back",gettag() + ",onresume,是否在前台:" + isonforeground()); super.onresume(); if (isleave) { isleave = false; timeoutcheck(); } } @override protected void onstop() { super.onstop(); if (!isonforeground()){ if (!isleave && isopenalp()) { isleave = true; savestarttime(); } } } public void timeoutcheck() { long endtime = system.currenttimemillis(); if (endtime - getstarttime() >= constant.timeout_alp * 1000) { util.toast(this, "超时了,请重新验证"); string alp = pref.getstring(constant.alp, null); if (alp == null || alp == "") { } else { intent intent = new intent(this, unlockgesturepasswordactivity.class); intent.putextra("pattern", alp); intent.putextra("login",false); //手势验证,不进行登录验证 intent.setflags(intent.flag_activity_clear_task); // 打开新的activity startactivityforresult(intent, constant.req_compare_pattern_timeout_check); } } } public void savestarttime() { pref.edit().putlong(constant.start_time, system.currenttimemillis()).commit(); } public long getstarttime() { long starttime = 0; try { starttime = pref.getlong(constant.start_time, 0); }catch (exception e){ starttime = 0; } return starttime; } /** * 程序是否在前端运行,通过枚举运行的app实现。防止重复超时检测多次,保证只有一个activity进入超时检测 *当用户按home键时,程序进入后端运行,此时会返回false,其他情况引起activity的stop函数的调用,会返回true * @return */ public boolean isonforeground() { activitymanager activitymanager = (activitymanager) getapplicationcontext().getsystemservice(context.activity_service); string packagename = getapplicationcontext().getpackagename(); list<activitymanager.runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses(); if (appprocesses == null) return false; for (activitymanager.runningappprocessinfo appprocess : appprocesses) { if (appprocess.processname.equals(packagename) && appprocess.importance == activitymanager.runningappprocessinfo.importance_foreground) { return true; } } return false; } }
补充说明:
可以根据importance的不同来判断前台或后台,runningappprocessinfo 里面的常量imtance就是上面所说的前台后台,其实imoportance是表示这个app进程的重要性,因为系统回收时候,会根据imoportance来回收进程的。具体可以去看文档。
public static final int importance_background = 400//后台 public static final int importance_empty = 500//空进程 public static final int importance_foreground = 100//在屏幕最前端、可获取到焦点 可理解为activity生命周期的onresume(); public static final int importance_service = 300//在服务中 public static final int importance_visible = 200//在屏幕前端、获取不到焦点可理解为activity生命周期的onstart();