Android 11 中的软件包可见性
关于Android 11 中权限的变更请阅读谷歌官方的两篇文章: package-visibility和查询特定软件包
比如要查询是否安装国内一些常见 App,在AndroidManifest.xml中添加如下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <manifest package="com.example.game"> <queries>
<intent> <action android:name="android.intent.action.DIAL" /> </intent>
<package android:name="com.tencent.mm" /> <package android:name="com.eg.android.AlipayGphone" /> <package android:name="com.sina.weibo" /> <package android:name="com.tencent.mobileqq" /> <package android:name="com.autonavi.minimap" /> <package android:name="com.baidu.BaiduMap" /> <package android:name="com.tencent.map" /> </queries> </manifest>
|
.NET 代码
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
| public static class PlatformUtils { public const string WeChatPackage = "com.tencent.mm";
public static bool IsInstallApp(Context context, string packageName) { var packages = GetPackageList(context); return packages.Count(x => x.PackageName == packageName) > 0; }
public static IEnumerable<PackageInfo> GetPackageList(Context context) { try { if (context.PackageManager != null) { var packageInfos = context.PackageManager.GetInstalledPackages(PackageInfoFlags.Activities | PackageInfoFlags.Services); return packageInfos.ToList(); } } catch (Java.Lang.Exception e) { e.PrintStackTrace(); } return new List<PackageInfo>(); }
public static List<string> GetPackageNameList() { List<string> packages = new(); try { var p = Runtime.GetRuntime()?.Exec("pm list packages"); if (p != null) { InputStreamReader isr = new(p.InputStream, "utf-8"); BufferedReader br = new(isr); var line = br.ReadLine(); while (line != null) { line = line.Trim(); if (line.Length > 8) { var prefix = line.Substring(0, 8); if (prefix.Equals("package:", StringComparison.OrdinalIgnoreCase)) { line = line[8..].Trim(); if (!TextUtils.IsEmpty(line)) { packages.Add(line); } } } line = br.ReadLine(); } br.Close(); }
p?.Destroy(); } catch (Java.Lang.Exception e) { e.PrintStackTrace(); } return packages; } }
|