记录Activity启动时间 ActivityLifecycleCallbacks

Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

目录

小知识点

应用进入前后台判断

getApplication().registerActivityLifecycleCallbacks(new SampleActivityLifecycleCallbacks() {
private int mFinalCount; @Override
public void onActivityStarted(Activity activity) {
super.onActivityStarted(activity);
mFinalCount++;
if (mFinalCount == 1){
//如果mFinalCount ==1,说明是从后台到前台
}
} @Override
public void onActivityStopped(Activity activity) {
super.onActivityStopped(activity);
mFinalCount--;
if (mFinalCount == 0){
//如果mFinalCount ==0,说明是前台到后台
}
}
}

获取自己App中的所有Activity

try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
for (ActivityInfo activity : packageInfo.activities) {
Log.i("bqt", activity.name); //com.bqt.test.MainActivity
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

获取栈顶和栈底的Activity

ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = manager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo info : list) {
Log.i("bqt", info.topActivity.getClassName() + "---" + info.baseActivity.getClassName());
}

Activity管理器

ActivityStackManager

定义一个集合(Stack)保存所有还未销毁的 Activity

public class ActivityStackManager {
private Stack<ActivityInfo> activityStack; private static ActivityStackManager instance = new ActivityStackManager(); private ActivityStackManager() {//构造方法私有
} public static ActivityStackManager getInstance() {
return instance;
} //入栈
public void push(Activity activity) {
if (activityStack == null) activityStack = new Stack<>(); for (int i = 0; i < activityStack.size(); i++) {
if (activity == activityStack.get(i).activity) return;
}
activityStack.add(ActivityInfo.newBuilder()
.activity(activity)
.startTime(System.currentTimeMillis())
.build());
} //出栈
public void pop(Activity activity) {
if (activityStack != null && activityStack.size() > 0 && activity != null) {
activity.finish();
activityStack.remove(new ActivityInfo(activity));
}
} //第一种方式
public ActivityInfo getActivityInfo(Activity activity) {
ActivityInfo activityInfo = null;
if (activityStack != null && activityStack.size() > 0) {
for (ActivityInfo info : activityStack) {
if (info.activity == activity) {
activityInfo = info;
break;
}
}
}
return activityInfo;
} //第二种方式,不建议采用这种方式,因为在使用中发现可能会报数组越界异常
//原因可能是当我通过search查找到之后和通过get获取之前这段时间,某个Activity被移除掉了,所以导致数组大小改变,取值时就可能越界了
public ActivityInfo getActivityInfo2(Activity activity) {
ActivityInfo activityInfo = null;
if (activityStack != null && activityStack.size() > 0) {
int index = activityStack.search(new ActivityInfo(activity));
if (index >= 0) {
activityInfo = activityStack.get(index);
}
}
return activityInfo;
} public long getStartTime(Activity activity) {
ActivityInfo activityInfo = getActivityInfo(activity);
return activityInfo != null ? activityInfo.startTime : 0;
}
}

registerActivityLifecycleCallbacks

注册 Activity 生命周期回调

registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
ActivityStackManager.getInstance().push(activity);
} @Override
public void onActivityDestroyed(Activity activity) {
long time = ActivityStackManager.getInstance().getStartTime(activity);
String date = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss SSS", Locale.getDefault()).format(new Date(time));
Log.i("bqt", "【" + activity.getClass().getSimpleName() + "启动时间】" + date);
ActivityStackManager.getInstance().pop(activity);//必须放在获取消息后面,否则一经清空了就获取不到了
} @Override
public void onActivityStarted(Activity activity) {
} @Override
public void onActivityResumed(Activity activity) {
Log.i("bqt", "当前Activity" + "(" + activity.getClass().getSimpleName()+".java" + ":" +30 + ")");
} @Override
public void onActivityPaused(Activity activity) {
} @Override
public void onActivityStopped(Activity activity) {
} @Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
});

ActivityInfo

保存与Activity相关的一些信息

//用于保存Activity示例以及与Activity相关的一些信息,比如Activity启动时间等
public class ActivityInfo {
public Activity activity;
public long startTime; public ActivityInfo(Activity activity) {
this.activity = activity;
} @Override
public boolean equals(Object obj) {//必须重写equals方法
return obj instanceof ActivityInfo && ((ActivityInfo) obj).activity.equals(activity);
} //省略Builder代码
}

一个通用的AppManager
/**
* activity堆栈式管理
*/
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到堆栈
*/
public void addActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<>();
}
activityStack.add(activity);
} /**
* 获取当前Activity(堆栈中最后一个压入的)
*/
public Activity currentActivity() {
return activityStack.lastElement();
} /**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public void finishActivity() {
Activity activity = activityStack.lastElement();
finishActivity(activity);
} /**
* 结束指定的Activity
*/
public void finishActivity(Activity activity) {
if (activity != null && !activity.isFinishing()) {
activityStack.remove(activity);
activity.finish();
activity = null;
}
} /**
* 结束指定类名的Activity
*/
public void finishActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
break;
}
}
} /**
* 结束所有Activity
*/
public void finishAllActivity() {
for (int i = 0, size = activityStack.size(); i < size; i++) {
if (null != activityStack.get(i)) {
finishActivity(activityStack.get(i));
break;
}
}
activityStack.clear();
} /**
* 获取指定的Activity
*/
public static Activity getActivity(Class<?> cls) {
if (activityStack != null)
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
return activity;
}
}
return null;
} public static Stack<Activity> getActivitys() {
return activityStack;
} /**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
android.os.Process.killProcess(android.os.Process.myPid());// 杀死该应用进程
System.exit(0);
} catch (Exception ignored) {
}
} /**
* 返回当前Activity栈中Activity的数量
*/
public int getActivityCount() {
int count = activityStack.size();
return count;
} /**
* 堆栈中移除Activity
*/
public void removeActivity(Activity activity) {
if (activityStack == null) {
return;
} else if (activityStack.contains(activity)) {
activityStack.remove(activity);
}
if (activity != null && !activity.isFinishing()) {
activity.finish();
activity = null;
}
}
}

2018-5-30

上一篇:关于整型Integer、Int32、Int64、IntPtr、UINT、UInt32、Cardinal、UInt64、UIntPtr、NativeUInt、Pointer、Handle


下一篇:解析-ESP01模块开发Arduino物联网wifi开关模块