AppUtil.java
4.06 KB
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
package com.cnlive.strike.util;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
/**
* Created by gujun on 2017/10/9.
*/
public class AppUtil {
private static final String TAG = "vlion AppUtil";
/**
* 判断某个界面是否在前台
*
* @param activity 要判断的Activity
* @return 是否在前台显示
*/
public static boolean isForeground(Activity activity) {
return isForeground(activity, activity.getClass().getName());
}
/**
* 判断某个界面是否在前台
*
* @param context Context
* @param className 界面的类名
* @return 是否在前台显示
*/
public static boolean isForeground(Context context, String className) {
if (context == null || TextUtils.isEmpty(className))
return false;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
if (list != null && list.size() > 0) {
ComponentName cpn = list.get(0).topActivity;
if (className.equals(cpn.getClassName()))
return true;
}
return false;
}
/**
* 获取包名
*
* @param context
* @return
*/
public static String getPackageName(Context context) {
return context.getPackageName();
}
/**
* get App versionName
*
* @param context
* @return
*/
public static String getVersionName(Context context) {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo;
String versionName = "";
try {
packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(versionName)) {
Log.e(TAG, "getVersionNameError: version name is null");
}
return versionName;
}
/**
* 判断当前应用是否是debug状态
*/
public static boolean isApkInDebug(Context context) {
try {
ApplicationInfo info = context.getApplicationInfo();
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (Exception e) {
return false;
}
}
/**
* get App versionName
*
* @param context
* @return
*/
public static int getVersionCode(Context context) {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo;
int versionCode = -1;
try {
packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionCode = packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
public static boolean checkPermissions(Context context, String[] PERMISSION) {
if (Build.VERSION.SDK_INT >= 23) for (String permisson : PERMISSION) {
if (TextUtils.isEmpty(permisson)) continue;
if (ActivityCompat.checkSelfPermission(context, permisson) == 0) continue;
return false;
}
return true;
}
/**
* 判定Content是否即将被销毁销毁
*
* @param context 上下文
* @return true 将被销毁 false 正常运行
*/
public static boolean isContextDestroyed(Context context) {
if (context == null) return true;
return context instanceof Activity && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && ((Activity) context).isDestroyed();
}
}