码迷,mamicode.com
首页 > 其他好文 > 详细

apk在线更新

时间:2018-09-08 11:51:14      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:tco   ndt   ast   html   weather   buffered   sed   app   fromfile   

package gejw.android.weather;

import gejw.android.weather.Constants.Constants;
import gejw.android.weather.piccl.MyLog;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

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;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class Update {
private Context context;
private int downLoadFileSize;
private ProgressDialog mpDialog;
private int fileSize;

//用的json文件进行版本更新 json文件内容 {"update":{"VersionInfo":"1.5"}}

public Update(Context c, int i) {
this.context = c;
String server_VersionInfo = "";//服务器版本号
String local_VersionInfo = getAppVersionName(context); //本地程序版本号
try {
String url = Constants.Update_URL + Constants.Update_html_Name;//服务器文件地址
BufferedReader in = new BufferedReader(new InputStreamReader(
new URL(url).openStream()));
String string;
while ((string = in.readLine()) != null) {
JSONObject jsonObject = new JSONObject(string.toString())
.getJSONObject("update");
server_VersionInfo = jsonObject.getString("VersionInfo");
// _115url=jsonObject.getString("update_url");
}
} catch (Exception e) {
// TODO: handle exception
}
if (Double.parseDouble(server_VersionInfo) > Double
.parseDouble(local_VersionInfo)) {
new AlertDialog.Builder(context)
.setMessage("检测到新版本"+server_VersionInfo+",是否要更新?")
.setNeutralButton("好的",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
// 进度框
mpDialog = new ProgressDialog(context);
mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置风格为长进度条
// mpDialog.setIcon(R.drawable.icon);//设置图标
// mpDialog.setMessage("正在下载中,请稍后");
mpDialog.setIndeterminate(false);// 设置进度条是否为不明确
// false
// 就是不设置为不明确
mpDialog.setCancelable(true);// 设置进度条是否可以按退回键取消
mpDialog.setProgress(0);
mpDialog.incrementProgressBy(1); // 增加和减少进度,这个属性必须的
mpDialog.show();
downLoadFile();
}
}).setPositiveButton("不要", null).show();

} else {
switch (i) {
case 1:
Toast.makeText(context, "无可用更新!", Toast.LENGTH_LONG).show();
break;
}

}

}

private void sendMsg(int flag) {
Message msg = new Message();
msg.what = flag;
handler.sendMessage(msg);
}

private final Handler handler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
// super.handleMessage(msg);
if (!Thread.currentThread().isInterrupted()) {
Log.i("msg what", String.valueOf(msg.what));
switch (msg.what) {
case 0:
// pbr.setMax(fileSize);
mpDialog.setMax(100);
break;
case 1:
int result = downLoadFileSize * 100 / fileSize;
mpDialog.setProgress(result);
break;
case 2:
// mpDialog.setMessage("文件下载完成");
openFile();
mpDialog.cancel();
break;
case -1:
String error = msg.getData().getString("error");
mpDialog.setMessage(error);
break;
default:
break;
}
}
super.handleMessage(msg);
}
};

private void openFile() {
// TODO Auto-generated method stub
File file = new File(Environment.getExternalStorageDirectory() + "/"
+ Constants.App_Name); //下载的apk文件保存地址
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}

private void downLoadFile() {
// TODO Auto-generated method stub
new Thread() {
public void run() {
String update_url = Constants.Update_URL + Constants.App_Name;
URL url = null;
try {
url = new URL(update_url);
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
InputStream in = con.getInputStream();
fileSize = con.getContentLength();
File fileOut = new File(
Environment.getExternalStorageDirectory() + "/"
+ Constants.App_Name);
FileOutputStream out = new FileOutputStream(fileOut);
byte[] bytes = new byte[1024];
downLoadFileSize = 0;
sendMsg(0);
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
downLoadFileSize += c;
sendMsg(1);// 更新进度条
}
in.close();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
new MyLog(e.toString());
}
sendMsg(2);// 下载完成
try {

} catch (Exception e) {
// TODO Auto-generated catch block
new MyLog(e.toString());
}
}
}.start();
}

public String getAppVersionName(Context context) {
String versionName = "";
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
// 这里的context.getPackageName()可以换成你要查看的程序的包名
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionName = pi.versionName;
if (versionName == null || versionName.length() <= 0) {
return "";
}
} catch (Exception e) {
new MyLog(e.toString());
}
return versionName;
}
}

apk在线更新

标签:tco   ndt   ast   html   weather   buffered   sed   app   fromfile   

原文地址:https://www.cnblogs.com/Sora-L/p/9608069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!