美文网首页
Android 应用更新 适配Android Q

Android 应用更新 适配Android Q

作者: MorningandSun | 来源:发表于2019-07-10 15:48 被阅读0次

1.通过后台检测版本信息,与本地版本信息进行对比。
贴出获取本地版本信息的代码

/**
     * 获取当前本地apk的版本
     *
     * @param mContext
     * @return
     */
    public static int getVersionCode(Context mContext) {
        int versionCode = 0;
        try {
            //获取软件版本号,对应AndroidManifest.xml下android:versionCode
            versionCode = mContext.getPackageManager().
                    getPackageInfo(mContext.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取版本号名称
     *
     * @param context 上下文
     * @return
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

2.弹窗提示用户进行下载(是否更新,看版本重要性,后台进行判断)
3.开启服务进行下载

public class UpdateService {
    //下载器
    private DownloadManager downloadManager;
    //上下文
    private Context mContext;
    //下载的ID
    private long downloadId;
    private  File file ;

    public UpdataService(Context context) {
        this.mContext = context;
    }

    //下载apk
    public void downloadAPK(String url) {
file = new File(mContext.getExternalFilesDir(null),"xxxx.apk");
            if (file.exists()) {
                file.delete();
            }
            //创建下载任务
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            //移动网络情况下是否允许漫游
            request.setAllowedOverRoaming(true);
            //在通知栏中显示,默认就是显示的
            request.setTitle("小鸟矿池");
            request.setDescription("小鸟矿池更新");

            request.setVisibleInDownloadsUi(true);
            //制定下载的文件类型为APK
            request.setMimeType("application/vnd.android.package-archive");
            //设置下载的路径
            request.setDestinationUri(Uri.fromFile(file));
            //获取DownloadManager
            downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            //将下载请求加入下载队列,加入下载队列后会给该任务返回一个long型的id,通过该id可以取消任务,重启任务、获取下载的文件等等
            downloadId = downloadManager.enqueue(request);

            //注册广播接收者,监听下载状态
            mContext.registerReceiver(receiver,
                    new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    //广播监听下载的各个状态
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkStatus();
        }
    };

    //检查下载状态
    private void checkStatus() {
        DownloadManager.Query query = new DownloadManager.Query();
        //通过下载的id查找
        query.setFilterById(downloadId);
        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            switch (status) {
                //下载暂停
                case DownloadManager.STATUS_PAUSED:
                    break;
                //下载延迟
                case DownloadManager.STATUS_PENDING:
                    Toast.makeText(mContext, "下载延迟", Toast.LENGTH_SHORT).show();
                    break;
                //正在下载
                case DownloadManager.STATUS_RUNNING:
                    Toast.makeText(mContext, "正在下载", Toast.LENGTH_SHORT).show();
                    break;
                //下载完成
                case DownloadManager.STATUS_SUCCESSFUL:
                    //下载完成安装APK
                    Toast.makeText(mContext,"下载完成",Toast.LENGTH_SHORT).show();
                    installAPK();
                    break;
                //下载失败
                case DownloadManager.STATUS_FAILED:
                    Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show();
                    mContext.unregisterReceiver(receiver);
                    break;
            }
        }
        c.close();
    }

    //下载到本地后执行安装
    private void installAPK() {
        //获取下载文件的Uri
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //7.0 以上需要FileProvider进行设置
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.N){
            Uri apkUri= FileProvider.getUriForFile(mContext,  "com.morningsun.xnpool.xnpool.fileprovider", file);
            intent.setDataAndType(apkUri, mContext.getContentResolver().getType(apkUri));
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }else{
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        mContext.startActivity(intent);
        mContext.unregisterReceiver(receiver);
    }
}

FileProvider设置
<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="xxxxxxxx.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <!-- 元数据 -->
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

xml文件

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path name="Download_app" path=""/>
</paths>

相关文章

网友评论

      本文标题:Android 应用更新 适配Android Q

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