|
| 1 | +# App相关 |
| 2 | +### 安装指定路径下的Apk |
| 3 | +``` java |
| 4 | +/** |
| 5 | +* 安装指定路径下的Apk |
| 6 | +*/ |
| 7 | +public void installApk(String filePath) { |
| 8 | + Intent intent = new Intent(); |
| 9 | + intent.setAction("android.intent.action.VIEW"); |
| 10 | + intent.addCategory("android.intent.category.DEFAULT"); |
| 11 | + intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive"); |
| 12 | + startActivityForResult(intent, 0); |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +### 卸载指定包名的App |
| 17 | +``` java |
| 18 | +/** |
| 19 | +* 卸载指定包名的App |
| 20 | +*/ |
| 21 | +public void uninstallApp(String packageName) { |
| 22 | + Intent intent = new Intent(); |
| 23 | + intent.setAction("android.intent.action.DELETE"); |
| 24 | + intent.addCategory("android.intent.category.DEFAULT"); |
| 25 | + intent.setData(Uri.parse("package:" + packageName)); |
| 26 | + startActivityForResult(intent, 0); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### 获取App名称 |
| 31 | +``` |
| 32 | +/** |
| 33 | +* 获取App名称 |
| 34 | +*/ |
| 35 | +public static String getAppName(Context context) { |
| 36 | + try { |
| 37 | + PackageManager packageManager = context.getPackageManager(); |
| 38 | + PackageInfo packageInfo = packageManager.getPackageInfo( |
| 39 | + context.getPackageName(), 0); |
| 40 | + int labelRes = packageInfo.applicationInfo.labelRes; |
| 41 | + return context.getResources().getString(labelRes); |
| 42 | + } catch (NameNotFoundException e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + return null; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +### 获取当前App版本号 |
| 51 | +``` java |
| 52 | +/** |
| 53 | +* 获取当前App版本号 |
| 54 | +*/ |
| 55 | +public static String getVersonName(Context context) { |
| 56 | + String versionName = null; |
| 57 | + PackageManager pm = context.getPackageManager(); |
| 58 | + PackageInfo info = null; |
| 59 | + try { |
| 60 | + info = pm.getPackageInfo(context.getApplicationContext().getPackageName(), 0); |
| 61 | + } catch (NameNotFoundException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + if (info != null) { |
| 65 | + versionName = info.versionName; |
| 66 | + } |
| 67 | + return versionName; |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### 打开指定包名的App |
| 72 | +``` |
| 73 | +/** |
| 74 | +* 打开指定包名的App |
| 75 | +*/ |
| 76 | +public void openOtherApp(String packageName){ |
| 77 | + PackageManager manager = getPackageManager(); |
| 78 | + Intent launchIntentForPackage = manager.getLaunchIntentForPackage(packageName); |
| 79 | + if (launchIntentForPackage != null) { |
| 80 | + startActivity(launchIntentForPackage); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### 打开指定包名的App应用信息界面 |
| 86 | +``` java |
| 87 | +/** |
| 88 | +* 打开指定包名的App应用信息界面 |
| 89 | +*/ |
| 90 | +public void showAppInfo(String packageName) { |
| 91 | + Intent intent = new Intent(); |
| 92 | + intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); |
| 93 | + intent.setData(Uri.parse("package:" + packageName)); |
| 94 | + startActivity(intent); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### 分享Apk信息 |
| 99 | +``` java |
| 100 | +/** |
| 101 | +* 分享Apk信息 |
| 102 | +*/ |
| 103 | +public void shareApkInfo(String info) { |
| 104 | + Intent intent = new Intent(); |
| 105 | + intent.setAction("android.intent.action.SEND"); |
| 106 | + intent.addCategory("android.intent.category.DEFAULT"); |
| 107 | + intent.setType("text/plain"); |
| 108 | + intent.putExtra(Intent.EXTRA_TEXT, info); |
| 109 | + startActivity(intent); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### 获取App信息的一个封装类(包名、版本号、应用信息、图标、名称等) |
| 114 | +``` java |
| 115 | +/** |
| 116 | +* 获取App信息的一个封装类(包名、版本号、应用信息、图标、名称等) |
| 117 | +*/ |
| 118 | +public class AppEnging { |
| 119 | + public static List<AppInfo> getAppInfos(Context context) { |
| 120 | + List<AppInfo> list = new ArrayList<AppInfo>(); |
| 121 | + //获取应用程序信息 |
| 122 | + //包的管理者 |
| 123 | + PackageManager pm = context.getPackageManager(); |
| 124 | + //获取系统中安装到所有软件信息 |
| 125 | + List<PackageInfo> installedPackages = pm.getInstalledPackages(0); |
| 126 | + for (PackageInfo packageInfo : installedPackages) { |
| 127 | + //获取包名 |
| 128 | + String packageName = packageInfo.packageName; |
| 129 | + //获取版本号 |
| 130 | + String versionName = packageInfo.versionName; |
| 131 | + //获取application |
| 132 | + ApplicationInfo applicationInfo = packageInfo.applicationInfo; |
| 133 | + int uid = applicationInfo.uid; |
| 134 | + //获取应用程序的图标 |
| 135 | + Drawable icon = applicationInfo.loadIcon(pm); |
| 136 | + //获取应用程序的名称 |
| 137 | + String name = applicationInfo.loadLabel(pm).toString(); |
| 138 | + //是否是用户程序 |
| 139 | + //获取应用程序中相关信息,是否是系统程序和是否安装到SD卡 |
| 140 | + boolean isUser; |
| 141 | + int flags = applicationInfo.flags; |
| 142 | + if ((applicationInfo.FLAG_SYSTEM & flags) == applicationInfo.FLAG_SYSTEM) { |
| 143 | + //系统程序 |
| 144 | + isUser = false; |
| 145 | + } else { |
| 146 | + //用户程序 |
| 147 | + isUser = true; |
| 148 | + } |
| 149 | + //是否安装到SD卡 |
| 150 | + boolean isSD; |
| 151 | + if ((applicationInfo.FLAG_EXTERNAL_STORAGE & flags) == applicationInfo.FLAG_EXTERNAL_STORAGE) { |
| 152 | + //安装到了SD卡 |
| 153 | + isSD = true; |
| 154 | + } else { |
| 155 | + //安装到手机中 |
| 156 | + isSD = false; |
| 157 | + } |
| 158 | + //添加到bean中 |
| 159 | + AppInfo appInfo = new AppInfo(name, icon, packageName, versionName, isSD, isUser); |
| 160 | + //将bean存放到list集合 |
| 161 | + list.add(appInfo); |
| 162 | + } |
| 163 | + return list; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// 封装软件信息的bean类 |
| 168 | +class AppInfo { |
| 169 | + //名称 |
| 170 | + private String name; |
| 171 | + //图标 |
| 172 | + private Drawable icon; |
| 173 | + //包名 |
| 174 | + private String packagName; |
| 175 | + //版本号 |
| 176 | + private String versionName; |
| 177 | + //是否安装到SD卡 |
| 178 | + private boolean isSD; |
| 179 | + //是否是用户程序 |
| 180 | + private boolean isUser; |
| 181 | + |
| 182 | + public AppInfo() { |
| 183 | + super(); |
| 184 | + } |
| 185 | + |
| 186 | + public AppInfo(String name, Drawable icon, String packagName, |
| 187 | + String versionName, boolean isSD, boolean isUser) { |
| 188 | + super(); |
| 189 | + this.name = name; |
| 190 | + this.icon = icon; |
| 191 | + this.packagName = packagName; |
| 192 | + this.versionName = versionName; |
| 193 | + this.isSD = isSD; |
| 194 | + this.isUser = isUser; |
| 195 | + } |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +### 判断当前App处于前台还是后台 |
| 200 | +``` java |
| 201 | +// 需添加<uses-permission android:name="android.permission.GET_TASKS"/> |
| 202 | +// 并且必须是系统应用该方法才有效 |
| 203 | +/** |
| 204 | +* 判断当前App处于前台还是后台 |
| 205 | +*/ |
| 206 | +public static boolean isApplicationBackground(final Context context) { |
| 207 | + ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 208 | + @SuppressWarnings("deprecation") |
| 209 | + List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); |
| 210 | + if (!tasks.isEmpty()) { |
| 211 | + ComponentName topActivity = tasks.get(0).topActivity; |
| 212 | + if (!topActivity.getPackageName().equals(context.getPackageName())) { |
| 213 | + return true; |
| 214 | + } |
| 215 | + } |
| 216 | + return false; |
| 217 | +} |
| 218 | +``` |
0 commit comments