[英]Install app automatically in android 7 or higher
I want to install app in my projects.我想在我的项目中安装应用程序。 But my code don't work in api 24 or higher.但是我的代码在 api 24 或更高版本中不起作用。 What is its solution?它的解决方案是什么?
My code is:我的代码是:
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);
}
}
Manifest:显现:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
This question is like my question.这个问题就像我的问题。 But does not work its solution for me!但对我来说它的解决方案不起作用!
Thanks about to help me!感谢即将帮助我!
Option 1:选项1:
You can easily launch a play store link or an install prompt:您可以轻松启动 Play 商店链接或安装提示:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content:///path/to/your.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);
or或者
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
startActivity(goToMarket);
However, you cannot install.apks without user's explicit permission;但是,没有用户的明确许可,您不能 install.apks; not unless the device and your program is rooted.除非设备和您的程序已植根,否则不会。
Option 2:选项 2:
Your choices are:您的选择是:
targetSdkVersion
to 23 or lower, or将你的targetSdkVersion
降到 23 或更低,或者For example:例如:
Intent i=new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
I resolved this problem and this way is true for me: First :我解决了这个问题,这种方式对我来说是正确的:首先:
private static final String APP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAppFolderInStorage/";
private void install() {
File file = new File(APP_DIR + fileName);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri downloadedApk = FileProvider.getUriForFile(getContext(), "ir.greencode", file);
intent.setDataAndType(downloadedApk, type);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(Uri.fromFile(file), type);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
getContext().startActivity(intent);
} else {
Toast.makeText(getContext(), "ّFile not found!", Toast.LENGTH_SHORT).show();
}
}
Second : For android 7 and above you should define a provider in manifest like below!第二:对于 android 7 及更高版本,您应该在清单中定义一个提供程序,如下所示!
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="ir.greencode"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
Third : Define path.xml in res/xml folder like below!第三:在 res/xml 文件夹中定义 path.xml,如下所示! I'm using this path for internal storage if you want to change it to something else there is a few way!如果您想将其更改为其他内容,我正在使用此路径进行内部存储,有几种方法! You can go to this FileProvider你可以 go 到这个FileProvider
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="your_folder_name" path="MyAppFolderInStorage/"/>
</paths>
Forth : You should add this permission in manifest:第四:您应该在清单中添加此权限:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.