版本更新

几乎每个应该都必备的功能
准备工作:请求版本更新获取能下载apk的链接,
比如:”http://yibanyue.oss-cn-hangzhou.xxx.com/apk/yiyue.apk
流程:请求版本更新链接–>获取到链接,弹框询问是否更新–>在本地建apk文件夹–>更新用DownloadManager下载apk,路径放在前面建好的文件夹中–>BroadcastReceiver监听是否下载好,下载完要激活安装
关键代码
弹框询问以及建文件夹,下载(无关代码自行删除):
DOWNLOAD_FOLDER_NAME:apk文件夹名字

DialogUtils.okAndCancel(mContext, pdesc, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //更新APP
            if (!TextUtils.isEmpty(url)) {
                downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                if (isForce) {
                    Dialog dialog = DialogUtils.progress(getContext(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            downloadManager.remove(SharedPreferencesUtils.getLong(getContext(), SPKey.APK_DOWN_ID, 0));
                            ActivityUtils.closeAll();
                        }
                    });
                }
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                File fileDir = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FOLDER_NAME);
                if (fileDir.exists() && fileDir.isDirectory()) {
                    request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, url.substring(url.lastIndexOf("/") + 1));
                } else {
                    fileDir.mkdirs();
                    request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, url.substring(url.lastIndexOf("/") + 1));
                }
                long downloadId = downloadManager.enqueue(request);
                downId = downloadId;
                SharedPreferencesUtils.saveLong(getContext(), SPKey.APK_DOWN_ID, downloadId);
            } else {
                toast(R.string.isEmptyUrl);
            }

        }
    }, new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isForce) {
                ActivityUtils.closeAll();
            }
        }
    });

下载完激活下载的apk:

public class DownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
        DownloadManager.Query query = new DownloadManager.Query();
        //在广播中取出下载任务的id
        long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
        query.setFilterById(id);
        Cursor c = manager.query(query);
        if (c.moveToFirst()) {
            //获取文件下载路径
            //Android 7.0以上的方式:请求获取写入权限,这一步报错


            int fileUriIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
            String fileUri = c.getString(fileUriIdx);
            String fileName = null;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                if (fileUri != null) {
                    fileName = Uri.parse(fileUri).getPath();
                }
            } else {
                //过时的方式:DownloadManager.COLUMN_LOCAL_FILENAME
                int fileNameIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
                fileName = c.getString(fileNameIdx);
            }


            if (id == SharedPreferencesUtils.getLong(context, SPKey.APK_DOWN_ID, 0) && !TextUtils.isEmpty(fileName)) {
                //打开APK安装
                installApk(context, fileName);
            } else {
                //如果文件名不为空,说明已经存在了,拿到文件名想干嘛都好
                if (fileName != null) {
                    UIUtils.toast(context, R.string.down_success);
                }
            }
        }
    } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {
        long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
        //点击通知栏取消下载
        manager.remove(ids);
        UIUtils.toast(context, R.string.down_cancel);
    }
}

/**
 * 安装APK文件
 */
public void installApk(Context context, String fileName) {
    ActivityUtils.closeAll();
    File apkfile = new File(fileName);
    if (!apkfile.exists()) {
        return;
    }
    // 通过Intent安装APK文件
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}}

记得在AndroidMainfest清单中添加:

<receiver android:name="com.meiguiyuehui.cn.receivers.DownReceiver">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
            <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
        </intent-filter>
    </receiver>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值