本文实例为大家分享了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);
}
abstract protected String getTag();
......省略号......
@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;
}
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();
您可能感兴趣的文章:Android线程管理之ActivityThreadAndroid Activity 横竖屏切换的生命周期详解Android Activity之间切换传递数据的方法Android动态加载Activity原理详解Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法详解详解Android开发中Activity的四种launchModeAndroid开发之TabActivity用法实例详解全面解析Android应用开发中Activity类的用法Android 暂停和恢复ActivityAndroid中子线程和UI线程通信详解android中UI主线程与子线程深入分析android开发教程之子线程中更新界面android使用handler ui线程和子线程通讯更新ui示例Android实现在子线程中更新Activity中UI的方法