使用okttputil网络请求框架检测更新
public
void
checkUpdate(Activity activity, final
boolean
isShowTip) { OkHttpUtils.get().url(Url.UPDATE_VERSION).build().execute( new
StringCallback() { @Override public
void
onError(Call call, Exception e, int
id) { } @Override public
void
onResponse(String response, int
id) { final
JsonData jsonData = JsonData.create(response); int
versionCode = jsonData.optInt( "version" ); int
currentVersionCode = SystemUtils.getAppVersionCode(context); if
(versionCode > currentVersionCode) { AlertDialog.Builder
builder = new
AlertDialog.Builder(context); builder.setTitle( "更新" ); builder.setMessage( "检测到有更新,是否立刻更新?" ); builder.setNegativeButton( "稍后更新" , new
DialogInterface.OnClickListener() { @Override public
void
onClick(DialogInterface dialog, int
which) { } }); builder.setPositiveButton( "立刻更新" , new
DialogInterface.OnClickListener() { @Override public
void
onClick(DialogInterface dialog, int
which) { if
(!StringUtils.isWifi(context)) { AlertDialog.Builder
builder = new
AlertDialog.Builder(context); builder.setTitle( "提示" ); builder.setMessage( "您当前正在使用移动网络,继续下载将消耗流量" ); builder.setNegativeButton( "取消" , new
DialogInterface.OnClickListener() { @Override public
void
onClick(DialogInterface dialog, int
which) { } }); builder.setPositiveButton( "确定" , new
DialogInterface.OnClickListener() { @Override public
void
onClick(DialogInterface dialog, int
which) { downLoadApk(jsonData.optString( "download" )); } }); builder.create().show(); } else
{ downLoadApk(jsonData.optString( "download" )); } } }); builder.create().show(); } else
{ if
(isShowTip) { ToastUtils.show(context, "当前已是最新版本" ); } } } }); } /* *
从服务器中下载APK */ protected
void
downLoadApk( final
String url) { final
ProgressDialog pd; //进度条对话框 pd
= new
ProgressDialog( this ); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage( "正在下载更新" ); pd.show(); new
Thread() { @Override public
void
run() { try
{ File
file = DownLoadManager.getFileFromServer(url, pd); sleep( 3000 ); installApk(file); pd.dismiss(); //结束掉进度条对话框 } catch
(Exception e) { Toast.makeText(context, "下载失败!" , Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }.start(); } //安装apk protected
void
installApk(File file) { Intent
intent = new
Intent(); //执行动作 intent.setAction(Intent.ACTION_VIEW); //执行的数据类型 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive" ); startActivity(intent); } |
这里是下载数据的代码DownLoadManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import
java.io.BufferedInputStream; import
java.io.File; import
java.io.FileOutputStream; import
java.io.InputStream; import
java.net.HttpURLConnection; import
java.net.URL; import
android.app.ProgressDialog; import
android.os.Environment; public
class
DownLoadManager { public
static
File getFileFromServer(String path, ProgressDialog pd) throws
Exception { //如果相等的话表示当前的sdcard挂载在手机上并且是可用的 if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { URL
url = new
URL(path); HttpURLConnection
conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout( 5000 ); //获取到文件的大小 pd.setMax(conn.getContentLength()); InputStream
is = conn.getInputStream(); File
file = new
File(Environment.getExternalStorageDirectory(), "某某某.apk" ); FileOutputStream
fos = new
FileOutputStream(file); BufferedInputStream
bis = new
BufferedInputStream(is); byte []
buffer = new
byte [ 1024 ]; int
len; int
total = 0 ; while
((len = bis.read(buffer)) != - 1 )
{ fos.write(buffer, 0 ,
len); total
+= len; //获取当前下载量 pd.setProgress(total); } fos.close(); bis.close(); is.close(); return
file; } else
{ return
null ; } } } |