wjc
2023-06-27 f8656588595af6cb716341b5daacba26e350a872
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.hdl.photovoltaic.utils;
 
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
 
import java.util.Iterator;
import java.util.Stack;
 
/**
 * Author: Zoro
 * Date: 2019/6/9
 * Description: This is AppManagerUtils
 */
 
public class AppManagerUtils {
 
    private static AppManagerUtils appManagerUtils;
 
    private AppManagerUtils() {
    }
 
    public static AppManagerUtils getAppManager() {
        if (null == appManagerUtils) {
            synchronized (AppManagerUtils.class) {
                if (null == appManagerUtils) {
                    appManagerUtils = new AppManagerUtils();
                }
            }
        }
        return appManagerUtils;
    }
 
    /**
     * 获取AppVersion
     *
     * @param context
     * @return
     */
    public static String getAppVersion(Context context) {
        String version = "";
        try {
            String packageName = context.getPackageName();
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            version = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }
 
    /**
     * 默认的Activity堆栈
     */
    private Stack<Activity> activityStack;
 
    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }
 
 
    /**
     * 移除Activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }
 
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?>... args) {
        Iterator<Activity> iterator = activityStack.listIterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            for (Class<?> cls : args) {
                if (activity.getClass().equals(cls)) {
                    activity.finish();
                    iterator.remove();
                }
            }
        }
    }
 
    /**
     * 指定一个类名,从指定类名开始移除后面所有Activity
     *
     * @param className   Activity-类名(activity.getClass().getName())
     * @param removeClass 是否移除本身(true-移除)
     */
    public void finishActivity(String className, boolean removeClass) {
        Iterator<Activity> iterator = activityStack.listIterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity.getClass().getName().equals(className)) {
                if (removeClass) {
                    activity.finish();
                    iterator.remove();
                }
                break;
            } else {
                activity.finish();
                iterator.remove();
            }
        }
 
//        int count= activityStack.size() - 1;
//        for (Iterator<Activity> it = activityStack.listIterator(count); it.hasNext(); ) {
//            Activity activity = it.next();
//            System.out.println("移除结束" + activity.getClass().getName());
//            if (activity.getClass().getName().equals(className)) {
//                if (removeClass) {
//                    activity.finish();
//                    it.remove();
//                }
//                break;
//            } else {
//                activity.finish();
//                it.remove();
//            }
//        }
    }
 
    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                Activity activity = activityStack.get(i);
                if (!activity.isFinishing()) {
                    activity.finish();
                }
            }
        }
        activityStack.clear();
    }
 
//    /**
//     * 判断当前应用是否是debug状态
//     */
//    public static boolean isApkInDebug(Context context) {
//        try {
//            ApplicationInfo info = context.getApplicationInfo();
//            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
//        } catch (Exception e) {
//            return false;
//        }
//    }
 
 
}