如何检测用户是否已确认在PackageInstaller

如何检测用户是否已确认在PackageInstaller

本文介绍了如何检测用户是否已确认在PackageInstaller(Android)中安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用PackageInstaller以编程方式安装android应用.

Background: I am installing android app programmatically using PackageInstaller.

我在做什么:下面提到了我正在使用的代码.这与Google在PackageInstaller示例中提供的代码相同.

What am I doing: The code which I am using is mentioned below. This is the same code which has been provided in PackageInstaller sample by google.

        private void init() {
        PackageInstaller.Session session = null;
                try {
                    PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
                    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
                    int sessionId = packageInstaller.createSession(params);
                    session = packageInstaller.openSession(sessionId);
                    addApkToInstallSession("HelloActivity.apk", session);
                    // Create an install status receiver.
                    Context context = InstallApkSessionApi.this;
                    Intent intent = new Intent(context, InstallApkSessionApi.class);
                    intent.setAction(PACKAGE_INSTALLED_ACTION);
                    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
                    IntentSender statusReceiver = pendingIntent.getIntentSender();
                    // Commit the session (this will start the installation workflow).
                    session.commit(statusReceiver);
                } catch (IOException e) {
                    throw new RuntimeException("Couldn't install package", e);
                } catch (RuntimeException e) {
                    if (session != null) {
                        session.abandon();
                    }
                    throw e;
                }
            }
        });
    }

        private void addApkToInstallSession(String assetName, PackageInstaller.Session session)
            throws IOException {
        // It's recommended to pass the file size to openWrite(). Otherwise installation may fail
        // if the disk is almost full.
        try (OutputStream packageInSession = session.openWrite("package", 0, -1);
             InputStream is = getAssets().open(assetName)) {
            byte[] buffer = new byte[16384];
            int n;
            while ((n = is.read(buffer)) >= 0) {
                packageInSession.write(buffer, 0, n);
            }
        }
    }
    // Note: this Activity must run in singleTop launchMode for it to be able to receive the intent
    // in onNewIntent().
    @Override
    protected void onNewIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        if (PACKAGE_INSTALLED_ACTION.equals(intent.getAction())) {
            int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
            String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
            switch (status) {
                case PackageInstaller.STATUS_PENDING_USER_ACTION:
                    // This test app isn't privileged, so the user has to confirm the install.
                    Intent confirmIntent = (Intent) extras.get(Intent.EXTRA_INTENT);
                    startActivity(confirmIntent);
                    break;
                case PackageInstaller.STATUS_SUCCESS:
                    Toast.makeText(this, "Install succeeded!", Toast.LENGTH_SHORT).show();
                    break;
                case PackageInstaller.STATUS_FAILURE:
                case PackageInstaller.STATUS_FAILURE_ABORTED:
                case PackageInstaller.STATUS_FAILURE_BLOCKED:
                case PackageInstaller.STATUS_FAILURE_CONFLICT:
                case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
                case PackageInstaller.STATUS_FAILURE_INVALID:
                case PackageInstaller.STATUS_FAILURE_STORAGE:
                    Toast.makeText(this, "Install failed! " + status + ", " + message,
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(this, "Unrecognized status received from installer: " + status,
                            Toast.LENGTH_SHORT).show();
            }
        }
    }

查询:该代码可以正常运行,但是 PackageInstaller 在单击安装"按钮时没有任何状态,而在单击取消"按钮时它提供了状态.当用户通过单击安装按钮确认安装时,我必须执行一些操作.单击安装按钮后,还有其他方法可以获取状态吗?

Query: The code works perfectly fine but PackageInstaller doesn't give any status on clicking the INSTALL button where as onclicking the CANCEL button, it provides status. I have to perform some action when user confirms installation by clicking install button. Is there any other way to get the status when install button is clicked?

注意:我不希望安装成功后的状态,但是单击安装按钮时希望它.

Note: I don't want the status after installation is successful but I want it when INSTALL button is clicked.

推荐答案

您可以注册广播操作 PACKAGE_ADDED ,甚至可以使用 PackageManager#getPackageInfo

You can register a broadcast for action PACKAGE_ADDED and even check if the package is installed using PackageManager#getPackageInfo

请参阅以下问题:

在意识到您试图检索安装"页面上的点击事件后,而是选择按钮-我认为通过任何良好的方式都是不可能的.您可以更改应用程序的流程并响应安装成功,也可以等待其他人来回答这个问题.他们可能是一些骇客".做到这一点的方法,但我仍然不建议使用它.

After realizing that you are trying to retrieve the click event on the "Install" button instead- I don't think it's possible by any good means. You can change the flow of your app and respond to the success of the installation instead or maybe wait for someone else to pick on this question. They might be some "hacky" way to do this but I'd still not recommend going with it.

这篇关于如何检测用户是否已确认在PackageInstaller(Android)中安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:58