使用OkGo向服务器发送检测版本请求:
private void checkVersion() {
OkGo.<String>get(Web.url + Web.version).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
version = response.body().split("<Version>")[1].split("</Version>")[0].trim();
content = response.body().split("<Content>")[1].split("</Content>")[0].trim();
url = response.body().split("<Url>")[1].split("</Url>")[0].trim();
createTime = response.body().split("<CreateTime>")[1].split("</CreateTime>")[0].substring(0, 10).trim();
if (!version.equals(Util.getVerName(context))) {
new UpDataVersionDialog(context, content, url).show();
}
}
});
}
在浏览器中更新:
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(url);
intent.setData(content_url);
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
context.startActivity(intent);
在app内部更新:(贴出完整代码)
public class Update {
private static final int DOWNLOAD = 1;
private static final int DOWNLOAD_FINISH = 2;
private int progress;
private boolean cancelUpdate = false;
private Context mContext;
private ProgressBar mProgress;
public Update(Context context) {
this.mContext = context;
}
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWNLOAD:
mProgress.setProgress(progress);
break;
case DOWNLOAD_FINISH:
installApk();
break;
default:
break;
}
}
};
/**
* 下载等待窗口
*/
public void showDownloadDialog() {
AlertDialog.Builder builder = new Builder(mContext,AlertDialog.THEME_HOLO_LIGHT);
builder.setTitle("正在更新");
builder.setIcon(R.drawable.esccload);
builder.setMessage("请稍等···");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.softupdate_progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
builder.setView(v);
builder.setNegativeButton("取消下载",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
cancelUpdate = true;
}
});
builder.setCancelable(false);
builder.create().show();
downloadApk();
}
/**
* 开启下载线程
*/
private void downloadApk() {
new downloadApkThread().start();
}
/**
* 下载程序
*/
private class downloadApkThread extends Thread {
@Override
public void run() {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL("http://****.*****.com/app/escc.apk");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "escc");
FileOutputStream fos = new FileOutputStream(apkFile);
int count = 0;
byte buf[] = new byte[1024];
do {
int numread = is.read(buf);
count += numread;
progress = (int) (((float) count / length) * 100);
mHandler.sendEmptyMessage(DOWNLOAD);
if (numread <= 0) {
mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
break;
}
fos.write(buf, 0, numread);
} while (!cancelUpdate);
fos.close();
is.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
/**
* 安装apk
*/
private void installApk() {
File apkfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "escc");
if (!apkfile.exists()) {
return;
}
if (Build.VERSION.SDK_INT>=24){
Intent i = new Intent(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri apkUri =
FileProvider.getUriForFile(mContext, "com.yda.yiyunchain.fileprovider", apkfile);
i.setDataAndType(apkUri,
"application/vnd.android.package-archive");
mContext.startActivity(i);
}
//7.0以下兼容
else {
try {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
"application/vnd.android.package-archive");
mContext.startActivity(i);
}catch (Exception e){
Log.e("sssssssssssssssss",e.getMessage());
}
}
}
}
在Manifest中配置FileProvider(7.0以上安装):
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yda.yiyunchain.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<!--元数据-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在res下新建一个xml文件夹,再新建一个file_paths资源文件:
path属性要设置成" ",这样代表可以访问所有路径,name可以随意
<?xml version="1.0" encoding="utf-8"?>
<resource>
<paths>
<external-path path="" name="download"/>
</paths>
</resource>
具体有关安装细节请查看:http://blog.csdn.net/lmj623565791/article/details/72859156