代码示例:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { boolean b = getPackageManager().canRequestPackageInstalls(); if (b) { //应用程序已启用安装未知来源的应用程序 } else { //启用安装未知来源的应用程序 Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES); startActivityForResult(intent, 999); } }
代码示例:
long apkSize = // Get the size of the APK file in bytes. String packageName = // Get package name of the APK file. PackageInfo packageInfo = null; try { packageInfo = packageManager.getPackageInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (packageInfo != null) { long installedApkSize = new File(packageInfo.applicationInfo.sourceDir).length(); // Check if there is enough space to install the app. if ((apkSize - installedApkSize) > availableStorageSpace) { // Not enough space to install the app. } }
代码示例:
if (Build.VERSION.SDK_INT < MINIMUM_COMPATIBLE_VERSION) { // Device does not meet the minimum required version to run the app. }
代码示例:
String packageName = // Get package name of the APK file. try { getPackageManager().getPackageInfo(packageName, 0); // The application is already installed. } catch (PackageManager.NameNotFoundException e) { // The application is not yet installed. }