android app内部更新适配到8.0

本文详细介绍了App内部更新的功能实现过程,包括版本号获取、服务器校验、下载管理及安装等关键步骤,并针对不同Android版本提供了适配方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

app 内部跟新是app中必须要有的功能,在app出现改变时,app内部更新能以最快的速度将应用提升到最新版本。

步骤:

1、获取本地app的版本号

 int versionCode = 0;
        try {
            // 获取软件版本号,
            versionCode = this.getPackageManager().getPackageInfo(
                    getPackageName(), 0).versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

2、上传服务器检查是否为最新app,对返回的数据进行操作。如果不是最新,则做更新操作。

3、调用android手机的DownLoadManager进行apk的下载

 request = new DownloadManager.Request(Uri.parse(url));
        // 设置通知栏标题
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle("一儒快递");//
        request.setDescription("一儒快递开始下载");
        request.setAllowedOverRoaming(false);
        request.allowScanningByMediaScanner();// 可被媒体扫描到
        request.setVisibleInDownloadsUi(true);// 可见和管理
        File dir = getApplication().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        filePath = dir.toString() + "/" + "insurework" + getVersionCode() + ".apk";
        // 设置文件存放目录
        request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "insurework" + getVersionCode() + ".apk");
        did = downManager.enqueue(request);

需要获取下载的状态,所以需要在初始化的时候注册广播监听系统下载完成ACTION,注意:在销毁的时候

IntentFilter filter = new IntentFilter();
        filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
        receiver = new DownLoadCompleteReceiver();
        registerReceiver(receiver, filter);
 @Override
    protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
 }
自定义的广播接收者,在接收者中作版本适配
private class DownLoadCompleteReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() != null && intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                long downid = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                if (did == downid) {// 如果下载完毕,
                    try {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            //判读版本是否在7.0以上(7.0及之后的自动安装需要ContentPrivide来获取到下载的文件)
                            installApk();
                        } else if (VERSION.SDK_INT >= VERSION_CODES.O) {
                            boolean b = getPackageManager().canRequestPackageInstalls();
                            if (b) {
                                installApk();
                            } else {
                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, Constent.NEW_8_0_REQUEST);
                            }
                        } else {
                            //7.0以前的启动方法
                            Intent install = new Intent(Intent.ACTION_VIEW);
                            install.setDataAndType(downManager.getUriForDownloadedFile(downid), "application/vnd.android.package-archive");
                            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(install);
                        }
                    } catch (Exception e) {
                        System.out.println(e.getMessage().toString().trim());
                    }
                }
            } else if (intent.getAction().equals(
                    DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
                // 点击事件
                System.out.println("ACTION_NOTIFICATION_CLICKED");
            }
        }
    }

其中8.0系统需要申请权限

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Constent.NEW_8_0_REQUEST) {
            installApk();
        }
    }
7.0以上自动安装逻辑
private void installApk() {
    Uri apkUri = FileProvider.getUriForFile(context, "你的包名.fileprovider", new File(filePath));//在AndroidManifest中的android:authorities值
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    install.setDataAndType(apkUri, "application/vnd.android.package-archive");
    startActivity(install);
}

内容提供者:res下新建xml文件夹,新建file_paths.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <external-path
            name="download"
            path=""/>
    </paths>
</resources>

在AndroidManifest.xml设置Provide

 <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="你的包名.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>
结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值