用xutils3.0进行下载
2024-08-30 20:47:12
写的例子比较简单,是用xutils3.0来进行下载项目更新
1.先通过网络请求,判断版本是否要更新
2.若要更新,则弹出一个弹窗,我用的是系统自带的Dialog,将下载的版本号及下载的内容提示展示出来
3.当用户点击下载时,开始下载,下载时展示一个水平的进度条
4.下载完成后,进度条消失,调用系统安装apk
如下是代码 用到的xutils的jar包是3.3.32版本的
package com.item.weixin; import java.io.File; import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.Button; /**
* 设置xutils下载
*
* @author Administrator
*
*/
public class LoginActivity extends Activity {
private Button ll_update;
private ProgressDialog pDialog;
private String nowVersion;
private ProgressDialog progressDialog; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_login);
ll_update = (Button) findViewById(R.id.btn_login);
ll_update.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
checkUpdate();
}
});
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(
getPackageName(), 0);
nowVersion = packageInfo.versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 下载更新,
*/
protected void checkUpdate() {
// TODO Auto-generated method stub
proDialogShow(LoginActivity.this, "正在查询...");
RequestParams params = new RequestParams("url");
x.http().get(params, new Callback.CommonCallback<String>() { @Override
public void onCancelled(CancelledException arg0) {
// TODO Auto-generated method stub } @Override
public void onError(Throwable arg0, boolean arg1) {
// TODO Auto-generated method stub
PDialogHide();
System.out.println("提示网络错误");
} @Override
public void onFinished() {
// TODO Auto-generated method stub } @Override
public void onSuccess(String arg0) {
// TODO Auto-generated method stub
PDialogHide();
try {
JSONObject object = new JSONObject(arg0);
boolean success = object.getBoolean("succee");
if (success) {
String desc = object.getString("desc");
String downloadurl = object.getString("downloadurl");
String versionname = object.getString("versionname");
if (nowVersion.equals(versionname)) {
System.out.println("当前版本为最新,不用跟新");
} else {
// 不同,弹出更新提示对话框
setUpDialog(versionname, downloadurl, desc);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} /**
*
* @param versionname
* 地址中版本的名字
* @param downloadurl
* 下载包的地址
* @param desc
* 版本的描述
*/
protected void setUpDialog(String versionname, final String downloadurl,
String desc) {
// TODO Auto-generated method stub
AlertDialog dialog = new AlertDialog.Builder(this).setCancelable(false)
.setTitle("下载" + versionname + "版本").setMessage(desc)
.setNegativeButton("取消", null)
.setPositiveButton("下载", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
setDownLoad(downloadurl);
}
}).create();
dialog.show();
} /**
* 下载包
*
* @param downloadurl
* 下载的url
*
*/
@SuppressLint("SdCardPath")
protected void setDownLoad(String downloadurl) {
// TODO Auto-generated method stub
RequestParams params = new RequestParams(downloadurl);
params.setAutoRename(true);//断点下载
params.setSaveFilePath("/mnt/sdcard/demo.apk");
x.http().get(params, new Callback.ProgressCallback<File>() { @Override
public void onCancelled(CancelledException arg0) {
// TODO Auto-generated method stub } @Override
public void onError(Throwable arg0, boolean arg1) {
// TODO Auto-generated method stub
if(progressDialog!=null && progressDialog.isShowing()){
progressDialog.dismiss();
}
System.out.println("提示更新失败");
} @Override
public void onFinished() {
// TODO Auto-generated method stub } @Override
public void onSuccess(File arg0) {
// TODO Auto-generated method stub
if(progressDialog!=null && progressDialog.isShowing()){
progressDialog.dismiss();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "demo.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
} @Override
public void onLoading(long arg0, long arg1, boolean arg2) {
// TODO Auto-generated method stub
progressDialog.setMax((int)arg0);
progressDialog.setProgress((int)arg1);
} @Override
public void onStarted() {
// TODO Auto-generated method stub
System.out.println("开始下载");
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为水平进行条
progressDialog.setMessage("正在下载中...");
progressDialog.setProgress(0);
progressDialog.show();
} @Override
public void onWaiting() {
// TODO Auto-generated method stub }
});
} private void proDialogShow(Context context, String msg) {
pDialog = new ProgressDialog(context);
pDialog.setMessage(msg);
// pDialog.setCancelable(false);
pDialog.show();
} private void PDialogHide() {
try {
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
最新文章
- 【WCF】操作选择器
- 浅谈c#中的delegate和event了
- 【poj2065】 SETI
- Matlab学习:读取指定文件夹及其五级子文件夹内的文件
- mysql---整体备份和增量备份
- 使用DataReader
- [HeadFirst-HTMLCSS学习笔记][第十二章HTML5标记]
- VMware linux 增加根目录空间 (使用图形分区工具gparted LiveCd)
- hdu_1181_变形课(dfs)
- 百度导航试用 vs 高德导航
- 网络基础tcp/ip协议五
- Struct_2路径问题
- OpenMP基础----以图像处理中的问题为例
- 树上倍增求LCA及例题
- top 自动执行的shell脚本中,使用top -n 1 >; log.txt, 上电自动执行,文件无输出
- Bar 柱状图
- js前段开发工具
- MongoDB复制集技术
- HRBUST - 1818 石子合并 区间dp入门
- how to backup your system of Autel MS908 Pro