【问题标题】:Install app automatically in android 7 or higher在 android 7 或更高版本中自动安装应用程序
【发布时间】: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


【解决方案1】:

选项 1:

您可以轻松启动 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;除非设备和您的程序已root。

选项 2:

您的选择是:

  • 将您的 targetSdkVersion 降到 23 或更低,或者
  • 将您的内容放在内部存储中,然后useFileProvider 制作 它可选择性地用于其他应用

例如:

Intent i=new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));

i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);

【讨论】:

  • "content:///path/to/your.apk""content:///" + Environment.getExternalStorageDirectory() + "/to/" + appName + ".apk" 还是 "content:///" + dir + "/" + appName?
  • 是的,使用这个:Environment.getExternalStorageDirectory() + "/" + dir + "/" + appName)
  • 感谢 BADSHAH。但它在 android 7 或更高版本中不起作用。如何自动安装apk?
  • 您的答案中AUTHORITY 的价值是什么?请解释一下...
  • 下面这段代码是真的吗?清单:&lt;provider android:name="androidx.core.content.FileProvider" android:authorities="ir.aramooz.fileprovider" android:exported="false" android:grantUriPermissions="true"&gt; &lt;meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_path" /&gt; &lt;/provider&gt;
【解决方案2】:

我解决了这个问题,这种方式对我来说是正确的: 第一

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();
    }
}

第二:对于 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>

第三:在 res/xml 文件夹中定义 path.xml,如下所示!如果您想将其更改为其他内容,我正在使用此路径进行内部存储,有几种方法!你可以去这个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:您应该在清单中添加此权限:

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2011-11-09
    • 2012-09-26
    • 1970-01-01
    • 2018-02-18
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多