apk在線更新

package gejw.android.weather;html

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

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;android

import org.json.JSONObject;json

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;app

//用的json文件進行版本更新 json文件內容 {"update":{"VersionInfo":"1.5"}}ide

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() {oop

@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();ui

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

}

}

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; }}

相關文章
相關標籤/搜索