android实现版本升级,Android学习之----利用DownLoadManager实现版本升级

效果:

49cefeeac546c1e16e79083ef1fa6f5d.png

通知栏显示

ebd754568192ec5d77b69627a328cb94.png

结果

不写demo了 直接看代码  需要可以直接传入下载地址 使用即可

public class VersionUpdate {

private DownCompleteReceiver downCompleteReceiver;

private static VersionUpdate versionUpdate = new VersionUpdate();

public static VersionUpdate newInstance() {

return versionUpdate;

}

private VersionUpdate() {

}

public void gotoUpdate(Context context, String url) {

String appName=context.getResources().getString(R.string.app_name);

DownloadManager downloadManager = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.setTitle(appName);

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDescription(appName+"升级中...");

request.setVisibleInDownloadsUi(true);

long downloadId = downloadManager.enqueue(request);

downCompleteReceiver = new DownCompleteReceiver(downloadId);

context.registerReceiver(downCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

request.setDestinationInExternalPublicDir(appName, appName+".apk");

}

public void createDialogUpdate(final Context context, final String url) {

AlertDialog.Builder alert=new AlertDialog.Builder(context);

alert.setTitle("更新提示")

.setMessage("发现新版本,是否立即更新?")

.setCancelable(false).setNeutralButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

}).setPositiveButton("立即更新", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

gotoUpdate(context, url);

dialog.dismiss();

}

});

alert.create().show();

}

public class DownCompleteReceiver extends BroadcastReceiver {

long enqueueId;

public DownCompleteReceiver(long enqueueId) {

this.enqueueId = enqueueId;

}

@Override

public void onReceive(Context context, Intent intent) {

DownloadManager dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);

long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

LogcatUtils.d(DownCompleteReceiver.class.getSimpleName(), "id = " + id + " , ");

if (enqueueId != id) {

return;

}

DownloadManager.Query query = new DownloadManager.Query();

query.setFilterById(enqueueId);

Cursor c = dm.query(query);

if (c != null && c.moveToFirst()) {

int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);

// 下载失败也会返回这个广播,所以要判断下是否真的下载成功

if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

// 获取下载好的 apk 路径

String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));

// 提示用户安装

installAPP(Uri.parse("file://" + uriString), context);

}

c.close();

}

}

private void installAPP(Uri data, Context context) {

Intent promptInstall = new Intent(Intent.ACTION_VIEW)

.setDataAndType(data, "application/vnd.android.package-archive");

// FLAG_ACTIVITY_NEW_TASK 可以保证安装成功时可以正常打开 app

promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(promptInstall);

}

}

public void unregisterReceiver(Context context){

if(downCompleteReceiver!=null)

context.unregisterReceiver(downCompleteReceiver);

}

}

使用:  VersionUpdate.newInstance().createDialogUpdate(this,url);

最后记得 取消 广播VersionUpdate.newInstance().unregisterReceiver()

结束

写的粗糙  有问题往指出

原文:http://blog.csdn.net/xiaxiayige/article/details/51330153

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,我们可以利用DownloadManager实现下载功能。DownloadManagerAndroid系统提供的一个用于管理下载任务的类,它负责处理下载请求,管理和控制下载任务的状态。 实现下载功能的步骤如下: 1. 创建DownloadManager对象: ``` DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); ``` 2. 构建下载请求: ``` DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl)); request.setTitle("文件名"); // 设置下载文件的标题 request.setDescription("下载中"); // 设置下载的描述信息 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); // 设置下载通知的可见性 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "文件名"); // 设置下载文件的保存路径和文件名 ``` 3. 添加下载请求到DownloadManager队列中: ``` long downloadId = downloadManager.enqueue(request); ``` 4. 监听下载状态: ``` BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (id == downloadId) { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor cursor = downloadManager.query(query); if (cursor.moveToFirst()) { int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); if (status == DownloadManager.STATUS_SUCCESSFUL) { // 下载成功 } else if (status == DownloadManager.STATUS_FAILED) { // 下载失败 } else if (status == DownloadManager.STATUS_RUNNING) { // 下载中 } } cursor.close(); } } }; registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); ``` 通过以上步骤,我们可以利用DownloadManager实现下载功能。用户可以通过监听下载状态,获取下载任务的状态并进行相应的处理,例如在下载完成时显示通知,或者在下载失败时提示用户重新下载。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值