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>
阅读全文
0 0
- Android使用DownloadMange进行版本更新(兼容7.0)
- 使用DownloadManager进行版本更新(兼容7.0)
- Android 版本更新工具 兼容7.0
- App版本更新兼容7.0
- android apk版本更新,兼容android7.0
- Android 使用DownloadManager进行版本更新的完整方案
- Android 使用DownloadManager进行版本更新的完整方案
- APP版本更新(兼容6.0,7.0系统)
- Android actionBar兼容版本的使用
- Android版本更新使用DownloadManager
- Android 7.0 版本更新
- Android版本更新(适用于6.0/7.0)
- Android仿新版微信主界面(使用android-support-v7兼容包,兼容2.2及其以上版本)
- Android应用更新详解,兼容7.0
- android 兼容包(可兼容低版本系统)
- android 兼容包(可兼容低版本系统)
- android 版本兼容 @TargetApi
- Android 版本兼容
- 程序员面试金典——最大连续数列和___
- android学习路线
- 关于java和c++里面 “类 = 类” 的区别
- Python插入排序算法
- 06 OrangePiLite wifi驱动配置
- Android使用DownloadMange进行版本更新(兼容7.0)
- 扫描目标主机端口
- 计算机基础知识
- Hello World
- Zookeeper常用命令
- Onvif开发之Linux下gsoap的使用及移植
- 输出三角形面积和类型(难度:半颗星)
- 程序员面试金典——链式A+B
- android动画学习(二)之属性动画