簡體   English   中英

在 android 7 或更高版本中自動安裝應用程序

[英]Install app automatically in android 7 or higher

我想在我的項目中安裝應用程序。 但是我的代碼在 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" />

這個問題就像我的問題。 但對我來說它的解決方案不起作用!

感謝即將幫助我!

選項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);

但是,沒有用戶的明確許可,您不能 install.apks; 除非設備和您的程序已植根,否則不會。

選項 2:

您的選擇是:

  • 將你的targetSdkVersion降到 23 或更低,或者
  • 將您的內容放在內部存儲上,然后使用FileProvider使其有選擇地可供其他應用程序使用

例如:

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

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

我解決了這個問題,這種方式對我來說是正確的:首先

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,如下所示! 如果您想將其更改為其他內容,我正在使用此路徑進行內部存儲,有幾種方法! 你可以 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>

第四:您應該在清單中添加此權限:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM