Install and update Android adaptation 7.0 and above

    Recently, I am planning to add a download and update function to the APP. I thought it was simple and passed the test by myself. But after sending it to the test, I found that the apk download was completed and the installation page did not pop up. I was depressed.

    The original method is written like this:

    /**
     * android1.x-6.x
     *
     * @param path 文件的路径
     */
    public void startInstall(Context context, String path) {
        Intent install = new Intent(Intent.ACTION_VIEW);
        install.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive");
        install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(install);
    }

    After a round of inspection, it was found that it was a problem with the Provider added by Android 7.0. The absolute path of the file cannot be used, but it needs to be used. The Uri obtained through the provider is installed. The modified version is as follows:

    /**
     * android o 以上
     * @param context
     * @param path
     */
    public void startInstallTest(Context context, String path) {
        Uri uri = FileProvider.getUriForFile(context.getApplicationContext(), "your provider authority", new File(path));
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        if (MIUIUtils.isMIUI()) {
            ComponentName companent = new ComponentName("com.miui.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
            intent.setComponent(companent);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }

    The above found that the installation page cannot be popped up in the Xiaomi system, but others can install it. By printing the system log, I found that I am missing a Xiaomi-specific Component in the ActivityManager log, so I added it after judgment.

    The complete call is as follows:

private void checkInstall(String path) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            startInstallTest(this, path);
        } else {
            startInstall(this, path);
        }
    }

 

    By the way, add the tool class for judging miui:

public class MIUIUtils {
    // 检测MIUI
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    public static boolean isMIUI() {
        Properties prop = new Properties();
        boolean isMIUI;
        try {
            prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        isMIUI = prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
                || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;

        return isMIUI;
    }
}

    The above are the pits I encountered during the development, download and update process. Some mobile phones need to be authorized first, and preprocessing needs to be added. However, I have not encountered such models, so I will not discuss the scope of this time. In addition, I will attach Download the tools of the apk package

Guess you like

Origin blog.csdn.net/HeartCircle/article/details/107014520