80,351
社区成员
发帖
与我相关
我的任务
分享
package net.iwetao.init;
import net.iwetao.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class UpdateManager {
private Context mContext;
//返回的安装包url
private String apkUrl = "http://qiye.iwetao.net/clientAPI/iwetao_1.3.3.apk";
private Dialog downloadDialog;
/* 下载包安装路径 */
private static final String savePath = "sdcard/Download/";
private static final String saveFileName = "UpdateDemoRelease.apk";
/* 进度条与通知ui刷新的handler和msg常量 */
private ProgressBar mProgress;
private static final int DOWN_UPDATE = 1;
private static final int DOWN_OVER = 2;
private int progress;
private Thread downLoadThread;
private boolean interceptFlag = false;
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_UPDATE:
mProgress.setProgress(progress);
break;
case DOWN_OVER:
android.util.Log.i("down_over","-----------created-----------");
installApk();
break;
default:
break;
}
};
};
public UpdateManager(Context context) {
this.mContext = context;
}
//外部接口让主Activity调用
public void checkUpdateInfo(){
showNoticeDialog();
}
private void showNoticeDialog(){
final AlertDialog alg = new AlertDialog.Builder(mContext).create();
alg.show();
Window win = alg.getWindow();
win.setContentView(R.layout.common_alert_dialog_update);
Button positiveButton = (Button)win.findViewById(R.id.PositiveButton);
Button negativeButton = (Button)win.findViewById(R.id.NegativeButton);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alg.dismiss();
showDownloadDialog();
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
alg.dismiss();
}
});
}
private void showDownloadDialog(){
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("软件版本更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.update_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();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.show();
downloadApk();
}
private Runnable mdownApkRunnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(apkUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(savePath);
if(!file.exists()){
file.mkdir();
}
File ApkFile = new File(file,saveFileName);
FileOutputStream fos = new FileOutputStream(ApkFile);
//FileOutputStream fos = mContext.openFileOutput(ApkFile.getName(), Context.MODE_WORLD_READABLE);
//fos.flush();
fos.flush();
int count = 0;
byte buf[] = new byte[1024];
do{
int numread = is.read(buf);
count += numread;
progress =(int)(((float)count / length) * 100);
//更新进度
mHandler.sendEmptyMessage(DOWN_UPDATE);
android.util.Log.i("numread",""+numread);
if(numread <= 0){
//下载完成通知安装
android.util.Log.i("progress","------下载完成啦------");
mHandler.sendEmptyMessage(DOWN_OVER);
break;
}
fos.write(buf,0,numread);
}while(!interceptFlag);//点击取消就停止下载.
fos.close();
is.close();
} catch (MalformedURLException e) {
android.util.Log.i("--------MalformedURLException---------",e.toString());
} catch(IOException e){
android.util.Log.i("IOException",e.toString());
}
}
};
/*
* 下载apk
* @param url
*/
private void downloadApk(){
downLoadThread = new Thread(mdownApkRunnable);
downLoadThread.start();
}
/*
* 安装apk
* @param url
*/
private void installApk(){
File apkfile = new File(savePath+saveFileName);
android.util.Log.i("apkfile","-------"+apkfile);
if (!apkfile.exists()) {
return;
}
try
{
//以下代码执行错误,但是直接在下载的目录里面打开apk,又可以正常安装,也就是下载的apk数据应该是完整的
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)
{
android.util.Log.i("installApk Exception",e.toString());
}
}
}