我想在我的项目中安装应用程序。但是我的代码在api 24或更高版本中不起作用。它有什么解决方案?
我的代码是:
String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
String dir = Environment.getExternalStorageDirectory() + "/" + context.getResources().getString(R.string.cache_path);
String appName = appModel.getAppUrl().substring(appModel.getAppUrl().lastIndexOf('/') + 1, appModel.getAppUrl().length());
appName = timestamp + "_" + appName;
private void appInstaller(String dir, String appName) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
File file = new File(dir, appName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + dir + "/" + appName)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
清单:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
question就像我的问题。但是对我来说不起作用!
感谢您的帮助!
您可以轻松启动Play商店链接或安装提示:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content:///path/to/your.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);
或
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
startActivity(goToMarket);
但是,如果没有用户的明确许可,您将无法安装.apks;除非设备和您的程序已植根,否则不会。