版本更新

来源:互联网 发布:mac安装win7驱动程序 编辑:程序博客网 时间:2024/05/16 18:08
getLatestVersion();
private String url;
private String version;
Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
String data = (String) msg.obj;
parserjson(data);
break;
default:
break;
}
}

};

private void parserjson(String data) {
LogUtils.LOGI("tag", "======data===" + data);
JSONObject jsonObject;
try {
if (data == null) {
return;
}
jsonObject = new JSONObject(data);
String Code = jsonObject.optString("Code");
JSONObject Result = jsonObject.optJSONObject("Result");
String AppName = Result.optString("AppName");
String Version = Result.optString("Version");
url = Result.optString("Url");
String Notice = Result.optString("Notice");
String PubTime = Result.optString("PubTime");
showUpdataDialog();

} catch (JSONException e) {
e.printStackTrace();
}
}
public void check(String appVersion) {
final String httpUrl = "http://apitest.inwanr.com/public/up/check?apptype=1&version=" + appVersion;
new Thread(new Runnable() {

@Override
public void run() {

Message message = Message.obtain();
message.what = 1;
String jsonResult = request(httpUrl);
message.obj = jsonResult;
handler.sendMessage(message);

}
}).start();
}

public String request(String httpurl) {
BufferedInputStream bis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL urlObj = new URL(httpurl);
HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection();
httpConn.setDoInput(true);
// httpConn.setDoOutput(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == 200) {
bis = new BufferedInputStream(httpConn.getInputStream());
byte[] buffer = new byte[1024 * 8];
int c = 0;
while ((c = bis.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
return baos.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}

if (baos != null) {
baos.close();
}

} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

public void showUpdataDialog() {
AlertDialog.Builder builer = new Builder(this);
builer.setTitle("版本升级");
builer.setMessage("检测到有新的版本,是否更新");
// 当点确定按钮时从服务器上下载 新的apk 然后安装
builer.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
LogUtils.LOGI("tag", "下载apk,更新");
downLoadApk();
}
});
// 当点取消按钮时进行登录
builer.setNegativeButton("取消", null);
AlertDialog dialog = builer.create();
dialog.show();
}

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(), "updata.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;
}
}

/*
* 从服务器中下载APK
*/
protected void downLoadApk() {
// final DownloadManager downloadManager = (DownloadManager)
// getSystemService(Context.DOWNLOAD_SERVICE);
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 = getFileFromServer(url, pd);
// Uri uri = Uri.parse(url);
// DownloadManager.Request request = new Request(uri);
// long reference = downloadManager.enqueue(request);
sleep(3000);
installApk(file);
pd.dismiss(); // 结束掉进度条对话框
} catch (Exception e) {
}
}
}.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");// 编者按:此处Android应为android,否则造成安装不了
startActivity(intent);
}

/**
* 获取版本号 检查更新
*/
private void getLatestVersion() {
String appVersion = "";
PackageManager packageManager = getPackageManager();
PackageInfo info = null;
try {
info = packageManager.getPackageInfo(getPackageName(), 0);
} catch (NameNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
appVersion = info.versionName;
check(appVersion);
}

0 0
原创粉丝点击