Android Activity回收与操作超时处理
本文实例为大家分享了AndroidActivity回收与操作超时的处理,供大家参考,具体内容如下
1、Activity的回收
针对多个activity退出的处理
关键代码:
1)、新建活动管理类:
publicclassActivityCollector{ privatestaticList<Activity>activityList=newArrayList<Activity>(); publicstaticvoidaddActivity(Activityactivity){ activityList.add(activity); } publicstaticvoidremoveActivity(Activityactivity){ activityList.remove(activity); } publicstaticvoidfinishAllButLast(){ Activityactivity=activityList.get(activityList.size()-1); removeActivity(activity); for(ActivityactivityItem:activityList){ if(!activityItem.isFinishing()){ activityItem.finish(); } } activityList.clear(); activityList.add(activity); } publicstaticvoidfinishAll(){ for(Activityactivity:activityList){ if(!activity.isFinishing()){ activity.finish(); } } activityList.clear(); } }
2)、创建基类BaseActivity,并使所有的activity继承自该基类。在创建时,添加到活动管理器,销毁时,从活动管理器中移除。
publicclassBaseActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); ActivityCollector.addActivity(this); } @Override protectedvoidonDestroy(){ super.onDestroy(); ActivityCollector.removeActivity(this); } }
如果需要销毁所有activity,只需调用finishAll()即可
2、操作超时处理
原理:
1)、在activity的stop函数中,根据app进程IMPORTANCE_FOREGROUND判断app在前台或后台
2)、在activity的onResume函数中,做超时检查。
关键代码:
abstractpublicclassTimeOutCheckActivityextendsBaseActivity{ privatebooleanisLeave=false; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); pref=getSharedPreferences(Constant.CONFIG_NAME,Context.MODE_PRIVATE); } /** *回调函数,方便测试 *@return */ abstractprotectedStringgetTag(); ......省略号...... /*** *当用户使程序恢复为前台显示时执行onResume()方法,在其中判断是否超时. */ @Override protectedvoidonResume(){ //Log.i("Back",getTag()+",onResume,是否在前台:"+isOnForeground()); super.onResume(); if(isLeave){ isLeave=false; timeOutCheck(); } } @Override protectedvoidonStop(){ super.onStop(); if(!isOnForeground()){ if(!isLeave&&isOpenALP()){ isLeave=true; saveStartTime(); } } } publicvoidtimeOutCheck(){ longendtime=System.currentTimeMillis(); if(endtime-getStartTime()>=Constant.TIMEOUT_ALP*1000){ Util.toast(this,"超时了,请重新验证"); Stringalp=pref.getString(Constant.ALP,null); if(alp==null||alp==""){ }else{ Intentintent=newIntent(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); } } } publicvoidsaveStartTime(){ pref.edit().putLong(Constant.START_TIME,System.currentTimeMillis()).commit(); } publiclonggetStartTime(){ longstartTime=0; try{ startTime=pref.getLong(Constant.START_TIME,0); }catch(Exceptione){ startTime=0; } returnstartTime; } /** *程序是否在前端运行,通过枚举运行的app实现。防止重复超时检测多次,保证只有一个activity进入超时检测 *当用户按home键时,程序进入后端运行,此时会返回false,其他情况引起activity的stop函数的调用,会返回true *@return */ publicbooleanisOnForeground(){ ActivityManageractivityManager=(ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); StringpackageName=getApplicationContext().getPackageName(); List<ActivityManager.RunningAppProcessInfo>appProcesses=activityManager.getRunningAppProcesses(); if(appProcesses==null) returnfalse; for(ActivityManager.RunningAppProcessInfoappProcess:appProcesses){ if(appProcess.processName.equals(packageName) &&appProcess.importance==ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){ returntrue; } } returnfalse; } }
补充说明:
可以根据importance的不同来判断前台或后台,RunningAppProcessInfo里面的常量IMTANCE就是上面所说的前台后台,其实IMOPORTANCE是表示这个app进程的重要性,因为系统回收时候,会根据IMOPORTANCE来回收进程的。具体可以去看文档。
publicstaticfinalintIMPORTANCE_BACKGROUND=400//后台 publicstaticfinalintIMPORTANCE_EMPTY=500//空进程 publicstaticfinalintIMPORTANCE_FOREGROUND=100//在屏幕最前端、可获取到焦点可理解为Activity生命周期的OnResume(); publicstaticfinalintIMPORTANCE_SERVICE=300//在服务中 publicstaticfinalintIMPORTANCE_VISIBLE=200//在屏幕前端、获取不到焦点可理解为Activity生命周期的OnStart();