【发布时间】:2019-11-20 08:27:23
【问题描述】:
我想在我的项目中安装应用程序。 但是我的代码在 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 就像我的问题一样。但它对我不起作用!
感谢您对我的帮助!
【问题讨论】:
-
“它不起作用”有点通用。它会抛出任何错误吗?或者它只是运行你的代码而不做任何事情?您的控制台中有任何“不寻常”的日志吗?检查this答案
-
它到底是怎么不工作的?它也不应该安装应用程序,而只提示用户安装它们。
-
它的日志是:
file:///storage/emulated/0/ARMarket/2019.11.21.07.09.51_oloom1.apk exposed beyond app through Intent.getData() E/EGL_emulation: tid 2952: eglSurfaceAttrib(1354): error 0x3009 (EGL_BAD_MATCH)不知道行不通!?
标签: java android android-intent installation