美文网首页
安装app结束回调的returnCode的解读

安装app结束回调的returnCode的解读

作者: Guoke24 | 来源:发表于2018-11-11 10:44 被阅读0次
    • 如代码所示:
    PackageInstallObserver observer = new PackageInstallObserver();
    pm.installPackage(uri, observer, installFlags, null);
    

    调用系统函数 installPackage 进行apk安装的时候,
    第二个参数会传入一个观察者实例:observer,
    PackageInstallObserver 的声明和实现的代码:

    class PackageInstallObserver extends IPackageInstallObserver.Stub
    {
        @Override
        public void packageInstalled(String packagename, int returnCode) {
                。。。。。。
        }
    }
    

    当安装结束时,就会回调这个观察者 observer 的回调函数:
    packageInstalled,其中参数 returnCode 表示着不同的安装结果。
    这些不同的结果标识,定义在:
    /frameworks/base/core/java/android/content/pm/PackageManager.java
    例如:

    /**
         * Installation return code: this is passed to the
         * {@link IPackageInstallObserver} on success.
         *
         * @hide
         */
        @SystemApi
        public static final int INSTALL_SUCCEEDED = 1;
    
        /**
         * Installation return code: this is passed to the
         * {@link IPackageInstallObserver} if the package is already installed.
         *
         * @hide
         */
        @SystemApi
        public static final int INSTALL_FAILED_ALREADY_EXISTS = -1;
        ......
    /**
         * Installation parse return code: this is passed to the
         * {@link IPackageInstallObserver} if the parser did not find any
         * certificates in the .apk.
         *
         * @hide
         */
        @SystemApi
        public static final int INSTALL_PARSE_FAILED_NO_CERTIFICATES = -103;
    
    

    更详细的解释可以参考:
    packageInstalled函数的参数 returnCode 的源码解读

    在返回 结果标识的同时,PackageManager.java 还提供了解析结果标识的函数:
    调用:

    PackageManager pm = mContext.getPackageManager();
    pm.installStatusToString(returnCode);
    

    installStatusToString 函数定义在:
    /frameworks/base/core/java/android/content/pm/PackageManager.java

    public static String installStatusToString(int status, String msg) {
            final String str = installStatusToString(status);
            if (msg != null) {
                return str + ": " + msg;
            } else {
                return str;
            }
        }
    
    public static String installStatusToString(int status) {
            switch (status) {
                case INSTALL_SUCCEEDED: return "INSTALL_SUCCEEDED";
                case INSTALL_FAILED_ALREADY_EXISTS: return "INSTALL_FAILED_ALREADY_EXISTS";
            ......
    

    相关文章

      网友评论

          本文标题:安装app结束回调的returnCode的解读

          本文链接:https://www.haomeiwen.com/subject/mgazxqtx.html