几乎每个应该都必备的功能
准备工作:请求版本更新获取能下载apk的链接,
比如:”http://yibanyue.oss-cn-hangzhou.xxx.com/apk/yiyue.apk”
流程:请求版本更新链接–>获取到链接,弹框询问是否更新–>在本地建apk文件夹–>更新用DownloadManager下载apk,路径放在前面建好的文件夹中–>BroadcastReceiver监听是否下载好,下载完要激活安装
关键代码
弹框询问以及建文件夹,下载(无关代码自行删除):
DOWNLOAD_FOLDER_NAME:apk文件夹名字
DialogUtils.okAndCancel(mContext, pdesc, new View.OnClickListener() {
@Override
public void onClick(View v) {
//更新APP
if (!TextUtils.isEmpty(url)) {
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (isForce) {
Dialog dialog = DialogUtils.progress(getContext(), new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadManager.remove(SharedPreferencesUtils.getLong(getContext(), SPKey.APK_DOWN_ID, 0));
ActivityUtils.closeAll();
}
});
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
File fileDir = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FOLDER_NAME);
if (fileDir.exists() && fileDir.isDirectory()) {
request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, url.substring(url.lastIndexOf("/") + 1));
} else {
fileDir.mkdirs();
request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, url.substring(url.lastIndexOf("/") + 1));
}
long downloadId = downloadManager.enqueue(request);
downId = downloadId;
SharedPreferencesUtils.saveLong(getContext(), SPKey.APK_DOWN_ID, downloadId);
} else {
toast(R.string.isEmptyUrl);
}
}
}, new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isForce) {
ActivityUtils.closeAll();
}
}
});
下载完激活下载的apk:
public class DownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
DownloadManager.Query query = new DownloadManager.Query();
//在广播中取出下载任务的id
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
query.setFilterById(id);
Cursor c = manager.query(query);
if (c.moveToFirst()) {
//获取文件下载路径
//Android 7.0以上的方式:请求获取写入权限,这一步报错
int fileUriIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String fileUri = c.getString(fileUriIdx);
String fileName = null;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (fileUri != null) {
fileName = Uri.parse(fileUri).getPath();
}
} else {
//过时的方式:DownloadManager.COLUMN_LOCAL_FILENAME
int fileNameIdx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
fileName = c.getString(fileNameIdx);
}
if (id == SharedPreferencesUtils.getLong(context, SPKey.APK_DOWN_ID, 0) && !TextUtils.isEmpty(fileName)) {
//打开APK安装
installApk(context, fileName);
} else {
//如果文件名不为空,说明已经存在了,拿到文件名想干嘛都好
if (fileName != null) {
UIUtils.toast(context, R.string.down_success);
}
}
}
} else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {
long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
//点击通知栏取消下载
manager.remove(ids);
UIUtils.toast(context, R.string.down_cancel);
}
}
/**
* 安装APK文件
*/
public void installApk(Context context, String fileName) {
ActivityUtils.closeAll();
File apkfile = new File(fileName);
if (!apkfile.exists()) {
return;
}
// 通过Intent安装APK文件
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}}
记得在AndroidMainfest清单中添加: