Procházet zdrojové kódy

1.添加获得应用信息的工具类

master
wangwanlei před 11 měsíci
rodič
revize
ec6528c478

+ 165
- 0
app/src/main/java/com/xhly/manageapp/utils/ZJAPPUtils.java Zobrazit soubor

@@ -0,0 +1,165 @@
1
+package com.xhly.manageapp.utils;
2
+
3
+
4
+import android.content.Context;
5
+import android.content.Intent;
6
+import android.content.pm.PackageInfo;
7
+import android.content.pm.PackageManager;
8
+import android.graphics.Color;
9
+import android.net.Uri;
10
+import android.os.Build;
11
+
12
+import androidx.annotation.ColorInt;
13
+import androidx.core.content.FileProvider;
14
+
15
+import com.xhly.corelib.utils.LogUtils;
16
+
17
+import java.io.File;
18
+import java.net.Inet6Address;
19
+import java.net.InetAddress;
20
+import java.net.NetworkInterface;
21
+import java.net.SocketException;
22
+import java.util.Enumeration;
23
+import java.util.List;
24
+import java.util.stream.Collectors;
25
+
26
+/**
27
+ * Created by PSVMC on 16/8/1.
28
+ */
29
+public class ZJAPPUtils {
30
+
31
+    /**
32
+     * 获取版本号
33
+     *
34
+     * @return 当前应用的版本号
35
+     */
36
+    public static int getVersionCode(Context context) {
37
+        try {
38
+            PackageManager manager = context.getPackageManager();
39
+            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
40
+            int versionCode = info.versionCode;
41
+            return versionCode;
42
+        } catch (Exception e) {
43
+            return 0;
44
+        }
45
+    }
46
+
47
+    /**
48
+     * 获取版本号
49
+     *
50
+     * @return 当前应用的版本号
51
+     */
52
+    public static String getVersionName(Context context) {
53
+        try {
54
+            PackageManager manager = context.getPackageManager();
55
+            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
56
+            String versionName = info.versionName;
57
+            return versionName;
58
+        } catch (Exception e) {
59
+            return "未知";
60
+        }
61
+    }
62
+
63
+    public static void installApk(Context mContext, File apkFile) {
64
+        if (Build.VERSION.SDK_INT >= 24) {//判读版本是否在7.0以上
65
+            Uri apkUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", apkFile);//在AndroidManifest中的android:authorities值
66
+            Intent install = new Intent(Intent.ACTION_VIEW);
67
+            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
68
+            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
69
+            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//对目标应用临时授权该Uri所代表的文件
70
+            install.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
71
+            mContext.startActivity(install);
72
+        } else {
73
+            Intent install = new Intent(Intent.ACTION_VIEW);
74
+            install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
75
+            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
76
+            mContext.startActivity(install);
77
+        }
78
+    }
79
+
80
+    /**
81
+     * 检查APP是否安装
82
+     *
83
+     * @param context
84
+     * @param pkgName
85
+     * @return
86
+     */
87
+    public static boolean checkAppInstalled(Context context, String pkgName) {
88
+        if (pkgName == null || pkgName.isEmpty()) {
89
+            return false;
90
+        }
91
+        final PackageManager packageManager = context.getPackageManager();
92
+        List<PackageInfo> info = packageManager.getInstalledPackages(0);
93
+        if (info.isEmpty())
94
+            return false;
95
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
96
+            LogUtils.d("packageManager", info.stream().map(it->it.packageName).collect(Collectors.toList()).toString());
97
+        }
98
+        for (int i = 0; i < info.size(); i++) {
99
+            LogUtils.d("packageManager", info.get(i).packageName);
100
+            if (pkgName.equals(info.get(i).packageName)) {
101
+                return true;
102
+            }
103
+        }
104
+        return false;
105
+    }
106
+
107
+    public static void openApk(Context mContext, String packagename) {
108
+        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(packagename);
109
+        if (intent != null) {
110
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111
+            mContext.startActivity(intent);
112
+        }
113
+    }
114
+
115
+    /**
116
+     * 过滤蓝光
117
+     *
118
+     * @param blueFilterPercent 蓝光过滤比例[10-80]
119
+     */
120
+    public static @ColorInt
121
+    int getColor(int blueFilterPercent) {
122
+        int realFilter = blueFilterPercent;
123
+        if (realFilter < 10) {
124
+            realFilter = 10;
125
+        } else if (realFilter > 80) {
126
+            realFilter = 80;
127
+        }
128
+        int a = (int) (realFilter / 80f * 180);
129
+        int r = (int) (200 - (realFilter / 80f) * 190);
130
+        int g = (int) (180 - (realFilter / 80f) * 170);
131
+        int b = (int) (60 - realFilter / 80f * 60);
132
+        return Color.argb(a, r, g, b);
133
+    }
134
+
135
+    /**
136
+     * 获取ip地址
137
+     */
138
+    public static String getHostIP() {
139
+        String hostIp = null;
140
+        try {
141
+            Enumeration nis = NetworkInterface.getNetworkInterfaces();
142
+            InetAddress ia = null;
143
+            while (nis.hasMoreElements()) {
144
+                NetworkInterface ni = (NetworkInterface) nis.nextElement();
145
+                Enumeration<InetAddress> ias = ni.getInetAddresses();
146
+                while (ias.hasMoreElements()) {
147
+                    ia = ias.nextElement();
148
+                    if (ia instanceof Inet6Address) {
149
+                        continue;// skip ipv6
150
+                    }
151
+                    String ip = ia.getHostAddress();
152
+                    if (!"127.0.0.1".equals(ip)) {
153
+                        hostIp = ia.getHostAddress();
154
+                        break;
155
+                    }
156
+                }
157
+            }
158
+        } catch (SocketException e) {
159
+            LogUtils.d("yao", "SocketException");
160
+            e.printStackTrace();
161
+        }
162
+        return hostIp;
163
+    }
164
+}
165
+

Načítá se…
Zrušit
Uložit