apk自动更新
来源:互联网 发布:制造业大数据来源 编辑:程序博客网 时间:2024/06/06 00:15
package com.joey.downdemo;import android.app.DownloadManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.SharedPreferences;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.database.Cursor;import android.net.Uri;import android.os.Environment;import android.preference.PreferenceManager;import android.view.View;import android.webkit.MimeTypeMap;import java.io.File;/** * Created by ted on 2017/5/19. */public class VersionManager { private Context context; private AppVersion appVersion; private DownloadManager downloadManager; private SharedPreferences prefs; private static final String DL_ID = "update_app_id"; // 文件在sd卡的真实目录 private String apkAbsolutePath; public void setOnUpdateListener(OnUpdateListener listener) { this.listener = listener; } private OnUpdateListener listener; public void setAppVersion(AppVersion appVersion) { this.appVersion = appVersion; } private static VersionManager instance = null; public static VersionManager getInstance(Context context, AppVersion appVersion) { if (instance == null) { instance = new VersionManager(context, appVersion); } return instance; } private VersionManager(Context context, AppVersion appVersion) { this.appVersion = appVersion; this.context = context; apkAbsolutePath = getSDPath() + appVersion.getFilePath() + "/" + appVersion.getFileName(); downloadManager = (DownloadManager) context .getSystemService(Context.DOWNLOAD_SERVICE); prefs = PreferenceManager.getDefaultSharedPreferences(context); } public void checkUpdateInfo() { String localVersion = getVersionCode(context) + ""; // 这里只要两个版本不一致就更新,不考虑版本降级的情况... if (!localVersion.equals(appVersion.getVersionCode())) { listener.hasNewVersion(true); } else { listener.hasNewVersion(false); } } /** * 获取版本号(内部识别号) * * @param context * @return */ private int getVersionCode(Context context) { try { PackageInfo pi = context.getPackageManager().getPackageInfo( context.getPackageName(), 0); return pi.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return -1; } } /** * 下载文件 */ public void downLoad() { if (!isSdCardExist()) { listener.onError("文件无法下载请检测您的sd卡"); return; } if (isFileExist(apkAbsolutePath)) { // 如果文件已经存在则安装app installApk(); return; } // 开始下载 Uri resource = Uri.parse(appVersion.getApkUrl()); DownloadManager.Request request = new DownloadManager.Request(resource); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); request.setAllowedOverRoaming(false); // 设置文件类型 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap .getFileExtensionFromUrl(appVersion.getApkUrl())); request.setMimeType(mimeString); // 在通知栏中显示 request.setNotificationVisibility(View.VISIBLE); request.setVisibleInDownloadsUi(true); request.setDestinationInExternalPublicDir( "/" + appVersion.getFilePath() + "/", appVersion.getFileName()); request.setTitle("版本更新..."); long id = downloadManager.enqueue(request); // 保存id prefs.edit().putLong(DL_ID, id).commit(); // 注册广播监听下载 context.registerReceiver(receiver, new IntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } private void installApk() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(apkAbsolutePath)), "application/vnd.android.package-archive"); context.startActivity(intent); } private boolean isSdCardExist() { return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); } private String getSDPath() { if (isSdCardExist()) { return Environment.getExternalStorageDirectory() + "/"; } return null; } private boolean isFileExist(String path) { return new File(path).exists(); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(prefs.getLong(DL_ID, 0)); Cursor c = downloadManager.query(query); if (c.moveToFirst()) { int status = c.getInt(c .getColumnIndex(DownloadManager.COLUMN_STATUS)); switch (status) { case DownloadManager.STATUS_RUNNING: // 正在下载,不做任何事情 listener.onDownloading(); break; case DownloadManager.STATUS_SUCCESSFUL: // 下载完成首先取消注册广播,然后安装app listener.onSuccess(); context.unregisterReceiver(receiver); installApk(); break; case DownloadManager.STATUS_FAILED: // 下载失败 清除已下载的内容,重新下载 context.unregisterReceiver(receiver); listener.onError("下载失败,请重试"); downloadManager.remove(prefs.getLong(DL_ID, 0)); prefs.edit().clear().commit(); break; } } } }; /** * app版本信息 * * @author sunger * */ public static class AppVersion { // apk下载url private String apkUrl; // apk最新版本 private String versionCode; public String getVersionCode() { return versionCode; } public void setVersionCode(String versionCode) { this.versionCode = versionCode; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } private String fileName; public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } /** * 文件在sd卡的相对路径 */ private String filePath; public String getApkUrl() { return apkUrl; } public void setApkUrl(String apkUrl) { this.apkUrl = apkUrl; } } public interface OnUpdateListener { /** * 是否有新版本更新,如果为true这里开始调用 * * @param has */ void hasNewVersion(boolean has); /** * 正在开始下载 */ void onDownloading(); /** * 下载完成,并且安装成功 */ void onSuccess(); /** * 更新失败 * * @param msg * 失败的消息 */ void onError(String msg); }}
public void btnClick(View view) { VersionManager.AppVersion version = new VersionManager.AppVersion(); // 设置文件url version.setApkUrl("http://server.m.pp.cn/download/apk?query=%E6%AF%8F%E6%AD%A5%E8%BF%90%E5%8A%A8&ch=smweb&ch_src=sm&appId=6759470&custom=0&uc_param_str=frvecpeimintnidnut"); // 设置文件名 version.setFileName("new"); // 设置文件在sd卡的目录 version.setFilePath("update"); // 设置app当前版本号 version.setVersionCode(1.1 + ""); final VersionManager manager = VersionManager .getInstance(MainActivity.this, version); manager.setOnUpdateListener(new VersionManager.OnUpdateListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this, "下载成功等待安装", Toast.LENGTH_SHORT).show(); } @Override public void onError(String msg) { Toast.makeText(MainActivity.this, "更新失败", Toast.LENGTH_SHORT).show(); } @Override public void onDownloading() { Toast.makeText(MainActivity.this, "正在下载...", Toast.LENGTH_SHORT).show(); } @Override public void hasNewVersion(boolean has) { if (has) { Toast.makeText(MainActivity.this, "检测到有新版本", Toast.LENGTH_SHORT).show(); manager.downLoad(); } } }); manager.checkUpdateInfo(); }
阅读全文
0 0
- apk自动更新
- android 自动更新apk版本
- Android 自动更新APK
- 使用DownloadManager 自动更新apk
- android 自动更新apk版本
- android 自动更新apk版本
- Android 自动更新Apk组件封装
- 有关android apk的版本号能否自动更新?
- android apk版本检测和自动更新
- Android7.0以上自动更新安装apk
- android之apk自动更新解析包失败问题
- Android Apk自动更新service(直接拿来用)
- android利用service实现下载apk自动更新功能
- 自动更新--下载apk以及提示对话框的实现(3)
- Android Apk自动更新service(直接拿来用)
- Android Apk自动更新service(直接拿来用)转载
- 安卓apk自动更新提示解析包失败
- 自动更新
- JAVA中的反射
- 将EXE作为资源,然后在释放到磁盘上并运行该exe程序
- Mac 快捷键
- linux下获取时间函数
- 【Spark系列】Scala语法——case
- apk自动更新
- Powerdesigner逆向工程,数据库生成物理视图
- 出现( linker command failed with exit code 1)错误总结
- Ubuntu下将普通用户提升到root权限
- VS F12无法转到定义
- error C2039:“Grabber”:不是“pcl”的成员
- Ubuntu版 jdk配置
- Google 官方应用架构的最佳实践指南
- Java中对list集合进行排序