Android使用DownloadMange进行版本更新(兼容7.0)

来源:互联网 发布:邓艾和姜维谁厉害知乎 编辑:程序博客网 时间:2024/06/05 05:47

1.简单实现

直接上代码

package com.dyb.testcamerademo;import android.app.DownloadManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageManager;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.os.Bundle;import android.os.Environment;import android.support.v4.content.FileProvider;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.widget.Toast;import java.io.File;public class Main2Activity extends AppCompatActivity {    private Context mContext;    private DownloadManager mDownloadManager;    private long downloadId;    private boolean isForceUpdate = true;//是否强制升级    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        mContext = this;        initVersionUpdate();    }    private void initVersionUpdate() {        checkUpdate();    }    /**     * 检测软件更新     */    public void checkUpdate() {        if (isUpdate()) {            // 显示提示对话框            showNoticeDialog();        } else {            Toast.makeText(mContext, "没有新版", Toast.LENGTH_LONG).show();        }    }    /**     * 显示软件更新对话框     */    private void showNoticeDialog() {        // 更新        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);        dialog.setTitle("软件更新")                .setMessage("检测到新版本,立即更新吗")                .setPositiveButton("更新", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        dialog.dismiss();                        Toast.makeText(mContext, "正在通知栏下载中", Toast.LENGTH_SHORT).show();                        // 显示下载对话框                        showDownloadDialog();                    }                });        if (!isForceUpdate) {            dialog.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialog, int which) {                    dialog.dismiss();                }            });        }        dialog.setCancelable(false);        dialog.show();    }    /**     * 显示软件下载对话框     */    private void showDownloadDialog() {        //安装包路径 此处模拟为百度新闻APP地址        String downPath = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk";        //使用系统下载类        mDownloadManager = (DownloadManager) mContext.getSystemService(DOWNLOAD_SERVICE);        Uri uri = Uri.parse(downPath);        DownloadManager.Request request = new DownloadManager.Request(uri);        request.setAllowedOverRoaming(false);        //创建目录下载        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "百度.apk");        // 把id保存好,在接收者里面要用        downloadId = mDownloadManager.enqueue(request);        //设置允许使用的网络类型,这里是移动网络和wifi都可以        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);        //机型适配        request.setMimeType("application/vnd.android.package-archive");        //通知栏显示        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);        request.setTitle("测试APP");        request.setDescription("正在下载中...");        request.setVisibleInDownloadsUi(true);        mContext.registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    }    /**     * 判断是否有更新,需要跟后台产生信息交互     *     * @return     */    private boolean isUpdate() {        // 获取当前软件版本        int versionCode = getVersionCode(mContext);        // 调用方法获取服务器可用版本信息,此处模拟为大于当前版本的定值        int serviceCode = 2;        // 版本判断        if (serviceCode > versionCode) {            return true;        }        return false;    }    /**     * 获取本地软件版本号     *     * @param context     * @return     */    private int getVersionCode(Context context) {        int versionCode = 0;        try {            // 获取软件版本号,对应AndroidManifest.xml下android:versionCode            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;        } catch (PackageManager.NameNotFoundException e) {            e.printStackTrace();        }        return versionCode;    }    private BroadcastReceiver mReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            checkStatus();        }    };    /**     * 检查下载状态     */    private void checkStatus() {        DownloadManager.Query query = new DownloadManager.Query();        query.setFilterById(downloadId);        Cursor cursor = mDownloadManager.query(query);        if (cursor.moveToFirst()) {            int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));            switch (status) {                //下载暂停                case DownloadManager.STATUS_PAUSED:                    break;                //下载延迟                case DownloadManager.STATUS_PENDING:                    break;                //正在下载                case DownloadManager.STATUS_RUNNING:                    break;                //下载完成                case DownloadManager.STATUS_SUCCESSFUL:                    installAPK();                    break;                //下载失败                case DownloadManager.STATUS_FAILED:                    Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show();                    break;            }        }        cursor.close();    }    /**     * 7.0兼容     */    private void installAPK() {        File apkFile =                new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "百度.apk");        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            Uri apkUri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".fileprovider", apkFile);            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");        } else {            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");        }        mContext.startActivity(intent);    }}

配置文件AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.dyb.testcamerademo">    <!-- sd卡写权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <!-- 网络 -->    <uses-permission android:name="android.permission.INTERNET" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".Main2Activity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>        <provider            android:name="android.support.v4.content.FileProvider"            android:authorities="com.dyb.testcamerademo.fileprovider"            android:exported="false"            android:grantUriPermissions="true">            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths"/>        </provider>        <activity android:name=".MainActivity">        </activity>    </application></manifest>

res包下新建xml包,新建的file_paths.xml代码如下:

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android"><!--name属性的值可以随便写,别名path属性的值表示共享的具体位置,设置空就表示将整个SD卡进行共享 --><paths>    <external-path        name="camera_photos"        path=""/></paths></paths>
原创粉丝点击