本文实例为大家分享了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();