如何在Android Oreo中使用PackageManager canRequestPackageInstalls? [英] How to use PackageManager canRequestPackageInstalls in Android Oreo?

查看:217
本文介绍了如何在Android Oreo中使用PackageManager canRequestPackageInstalls?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用清单中,我声明了权限的使用:

In my app Manifest I have declared the use of the permission:

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

在我的代码中,我检查我的应用程序是否可以从未知来源安装:

and in my code I check if my app can install from unknown sources:

    public void reinstallApp(Activity activity, String pathname, int request_code)
    {
        if (activity.getPackageManager().canRequestPackageInstalls())
        {
            try
            {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(pathname)), "application/vnd.android.package-archive");
                activity.startActivityForResult(intent, request_code);
            }
            catch (Exception e)
            {
                LogUtilities.show(this, e);
            }
        }
        else
        {
            activity.startActivity(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", activity.getPackageName()))));
        }
    }

但是即使我在选择活动中检查了来自未知资源的允许安装,"activity.getPackageManager().canRequestPackageInstalls()"始终返回"false".

but the "activity.getPackageManager().canRequestPackageInstalls()" is always returning "false", even if I check the allow install from unknow sources in the selection activity.

出什么问题了?

推荐答案

您必须先请求权限.为此,您必须调用来自未知来源的安装许可.通过重新排列您的代码,我得到了答案.

You've to ask for permission first. For that you've to call install permission from unknown sources. I got the answer by just rearranging your code.

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (!getPackageManager().canRequestPackageInstalls()) {
                startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1234);
            } else {
                callInstallProcess();
            }
        } else {
            callInstallProcess();
        }

上面的代码将在您的onCreate()中.您可以验证结果.

The above code will be in your onCreate() . The you can verify the result.

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1234 && resultCode == Activity.RESULT_OK) {
        if (getPackageManager().canRequestPackageInstalls()) {
            callInstallProcess();
        }
    } else {
        //give the error
    }
}

在installInstallProcess()中进行安装的位置

Where your installation is happening in callInstallProcess();

        try
        {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(pathname)), "application/vnd.android.package-archive");
            activity.startActivityForResult(intent, request_code);
        }
        catch (Exception e)
        {
            LogUtilities.show(this, e);
        }

别忘了在AndroidManifest.xml中授予权限

Don't forget to give permission in AndroidManifest.xml

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

这篇关于如何在Android Oreo中使用PackageManager canRequestPackageInstalls?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆