Skip to content

解决使用默认的UpdateDialogFragment在初次安装应用时候,因为DownloadService没有能使用activity.s… #66

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Conversation

Jiiiiiin
Copy link
Contributor

@Jiiiiiin Jiiiiiin commented Apr 5, 2018

解决使用默认的UpdateDialogFragment在初 次安装应用时候,因为DownloadService没有能使用activity.startActivityForResult启动安装界面,导致用户如果取消安装,插件使用者得不到通知的问题:

private void startDownload(UpdateAppBean updateApp, final DownloadCallback callback) {

        mDismissNotificationProgress = updateApp.isDismissNotificationProgress();

        String apkUrl = updateApp.getApkFileUrl();
        if (TextUtils.isEmpty(apkUrl)) {
            String contentText = "新版本下载路径错误";
            stop(contentText);
            return;
        }
        String appName = AppUpdateUtils.getApkName(updateApp);

        File appDir = new File(updateApp.getTargetPath());
        if (!appDir.exists()) {
            appDir.mkdirs();
        }

        String target = appDir + File.separator + updateApp.getNewVersion();

        updateApp.getHttpManager().download(apkUrl, target, appName, new FileDownloadCallBack(callback));
    }

    private void stop(String contentText) {
        if (mBuilder != null) {
            mBuilder.setContentTitle(AppUpdateUtils.getAppName(DownloadService.this)).setContentText(contentText);
            Notification notification = mBuilder.build();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            mNotificationManager.notify(NOTIFY_ID, notification);
        }
        close();
    }

    private void close() {
        stopSelf();
        isRunning = false;
    }

    /**
     * 进度条回调接口
     */
    public interface DownloadCallback {
       // 新增下面的回调口子
        /**
         * 当应用处于前台,准备执行安装程序时候的回调,
         *
         * @param file
         * @return
         */
        boolean onInstallAppAndAppOnForeground(File file);
    }

在初次应用下载完毕:

 @Override
        public void onResponse(File file) {
            if (mCallBack != null) {
                if (!mCallBack.onFinish(file)) {
                    close();
                    return;
                }
            }

            try {

                if (AppUpdateUtils.isAppOnForeground(DownloadService.this) || mBuilder == null) {
                    //App前台运行
                    mNotificationManager.cancel(NOTIFY_ID);
                    **// 给应用开发者相应的权限去控制,是否自己处理**
                    boolean temp = mCallBack.onInstallAppAndAppOnForeground(file);
                    if (!temp) {
                        AppUpdateUtils.installApp(DownloadService.this, file);
                    }

比如,在默认的UpdateDialogFragment中:

 /**
     * 回调监听下载
     */
    private void startDownloadApp(DownloadService.DownloadBinder binder) {
        // 开始下载,监听下载进度,可以用对话框显示
        if (mUpdateApp != null) {

            binder.start(mUpdateApp, new DownloadService.DownloadCallback() {
               //....

                **@Override
                public boolean onInstallAppAndAppOnForeground(File file) {
                    // 如果应用处于前台,那么就自行处理应用安装
                    AppUpdateUtils.installApp(UpdateDialogFragment.this.getActivity(), file);
                    dismiss();
                    return true;
                }**
            });
        }
    }

这样插件使用者就能得到用户取消安装应用的通知:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                break;
            **case Activity.DEFAULT_KEYS_DIALER:
                switch (requestCode){
                    // 得到通过UpdateDialogFragment默认dialog方式安装,用户取消安装的回调通知,以便用户自己去判断,比如这个更新如果是强制的,但是用户下载之后取消了,在这里发起相应的操作
                    case AppUpdateUtils.REQ_CODE_INSTALL_APP:
                        Toast.makeText(this,"用户取消了安装包的更新", Toast.LENGTH_LONG).show();
                        break;
                }
                break;**
            case Activity.RESULT_CANCELED:
                break;
            default:
        }
    }

…tartActivityForResult启动安装界面,导致用户如果取消安装,插件使用者得不到通知的问题
@WVector WVector merged commit 3ec2793 into WVector:master Apr 5, 2018
@Jiiiiiin
Copy link
Contributor Author

Jiiiiiin commented Apr 5, 2018

(*°∀°)=3

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants