Android8.0应用程序更新

Android8.0应用程序更新

https://www.jianshu.com/p/85913ed97af5 android 在线更新
https://www.cnblogs.com/gooder2-android/p/8966045.html android 在线更新
https://www.cnblogs.com/gzdaijie/p/5222191.html android8种对话框

app在线更新是一个比较常见需求,新版本发布时,用户进入我们的app,就会弹出更新提示框,第一时间更新新版本app。
在线更新分为以下几个步骤:
1, 权限检查,查看是否具有未知来源应用安装权限,有权限立刻更新,无权限打开权限
2, 获取本地版本号与线上版本号获取线上版本号,versionCode
3, 比较线上的versionCode 和本地的versionCode,弹出更新窗口
4,更新窗口。
5, 下载APK文件(文件下载)
6,安装APK

  1. 第一步,查看是否具有未知来源应用安装权限

通过权限检查,我们可以得知自己的手机是否有未知来源应用安装权限,以及获取手机状态信息权限。(可能会在或许线上版本时用到)

https://blog.csdn.net/qq_40982049/article/details/90727428

  1. 第二步, 获取本地版本号与线上版本号获取线上版本号,versionCode
//获取本地版本
private void getlocalVersionCode() {
    try{
        //获取本地版本号
        localVersion =getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(),0).versionName;
    }catch (PackageManager.NameNotFoundException e)
    {
        e.printStackTrace();
    }
}
  1. 第三步,通过版本号对比是否有新版本
//检查是否有新版本
private boolean CompareVersion() {
    //获取本地版本
    getlocalVersionCode();
    //获取线上版本
    getServerVersionCode();
    boolean hasNewVersion =false ;
    String temp = NewVersion.substring(1);
    if (localVersion.equals(temp)) {
        hasNewVersion =false;
    }
    String[] newV = temp.split("\\.");
    String[] localV = localVersion.split("\\.");
    int iTotal = newV.length > localV.length ? newV.length : localV.length;
    for (int i = 0; i < iTotal; i++) {
        if (Integer.parseInt(newV[i]) > Integer.parseInt(localV[i])){
            hasNewVersion =true;
            break;
            }
        }
        return  hasNewVersion;
    }
  1. 第四步,弹出更新窗口
//下载与更新的对话框
private void showNormalDialog(){
    final AlertDialog.Builder normalDialog = new AlertDialog.Builder(getActivity());
    boolean HasNewVersionApk = CompareVersion();;
    normalDialog.setIcon(R.drawable.logo);
    normalDialog.setTitle(getString(R.string.Application_Update));
    if (!HasNewVersionApk){
        //无新版本 
        normalDialog.setMessage(getString(R.string.Update_TipNews)+localVersion);
        normalDialog.setNegativeButton(getString(R.string.Update_CancelBtn),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
    }else {
    	//有新版本
        normalDialog.setMessage(getString(R.string.Update_TipNews)+NewVersion);
        normalDialog.setPositiveButton(getString(R.string.Update_UpdatelBtn),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        AppUpdateManager updateManager = new AppUpdateManager(getActivity());
                        updateManager.downloadApk(Server+apkUri,"1.0.0.1","新版本");
                    }
                });
        normalDialog.setNegativeButton(getString(R.string.Update_CancelBtn),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

    }
    // 显示
    normalDialog.show();
}
  1. 第五步,下载与更新
//apk下载与更新类
public class AppUpdateManager {
    private  Activity mActivity;
    private DownloadManager mDownloadManager;
    private WeakReference<Activity> weakReference;
    private  DownloadReceiver  mDownloadReceiver;
    private long mReId;
    //构造函数
    public AppUpdateManager(Activity activity ) {
        mActivity = activity;
        weakReference = new WeakReference<Activity>(activity);
        mDownloadManager = (DownloadManager)weakReference.get().getSystemService(Context.DOWNLOAD_SERVICE);
        mDownloadReceiver= new DownloadReceiver();
    }
    //应用程序下载
    public void downloadApk(String apkUrl,String title,String desc) {
       //注册广播事件,待下载完成后通知程序自动安装(一定要配置,否则无法监听到应用程序下载成功)
        weakReference.get().registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        //查看apk文件是否存在,若存在则删除
       File apkFile = new File(weakReference.get().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),"Test.apk");
       if (apkFile !=null && apkFile.exists() )
       {
           apkFile.delete();
       }
       //新建下载请求
       DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
       //设置title,文件名
       request.setTitle(title);
       //设置新版本描述
       request.setDescription(desc);
       //完成后显示通知栏
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
       request.setDestinationInExternalFilesDir(weakReference.get(),Environment.DIRECTORY_DOWNLOADS,"Test.apk");
       request.setMimeType("application/vnd.android.package-archive");
       mReId = mDownloadManager.enqueue(request);
    }
    //更新完成通知广播
    class DownloadReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            //  检测是否下载完成
            long completeDownLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            if (completeDownLoadId == mReId) {
            //  下载完成,开始安装
                installApk(context);
            }
        }
    }
    //应用程序安装
    public void installApk(Context context) {
        try{
            Uri uri = null;
            Intent intentInstall = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            intentInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intentInstall.setAction(Intent.ACTION_VIEW);
            //查看apk文件是否存在,若存在则删除
            File apkFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),"Test.apk");
            if (apkFile.exists() )
            {
                //使用FileProvider需要在配置文件中配置,配置方法请看文章最后
                uri = FileProvider.getUriForFile(context,"包名.FileProvider",apkFile);          
                intentInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intentInstall.setDataAndType(uri, "application/vnd.android.package-archive");
               //打开程序安装界面
                context.startActivity(intentInstall);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public void  cancel(){
        mDownloadManager.remove(mReId);
    }

    public void  resume(){
        // 注册广播,监听APK是否下载完成
        weakReference.get().registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
    public void  onPause() {
        weakReference.get().unregisterReceiver(mDownloadReceiver);
    }

}
  1. 使用FileProvider的文件配置方法
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aly_D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值