//***************************************************************
//***************** 启动类 ***************************
//***************************************************************
//升级
protected void update() {
// 新版
Message msg = BroadcastHandler.obtainMessage();
BroadcastHandler.sendMessage(msg);
}
protected void updateAlertDialog() {
String str_UpdateMessage = "";
if(ConstMethod.b_forceToUpdate){
str_UpdateMessage = "很抱歉,当前版本已被停用。" + "\n" + "请您升级为最新版本("
+ Account.ver + ")" + "。" + "\n" + "描述:" + "\n" + Account.desc;
}else if(ConstMethod.b_update){
str_UpdateMessage = "当前版本还可用,目前最新的版本为:" + Account.ver + "。"
+ "\n" + "版本描述:" + "\n" + Account.desc;
}
myDialog = new AlertDialog.Builder(this).create();
myDialog.show();
myDialog.getWindow().setContentView(R.layout.update);
myDialog.setCancelable(false);
Button btn_ok = (Button) myDialog.findViewById(R.id.btn_ok);
Button btn_cancel = (Button) myDialog.findViewById(R.id.btn_cancel);
TextView tv_content = (TextView) myDialog.findViewById(R.id.tv_content);
final CheckBox cb_tip = (CheckBox) myDialog.findViewById(R.id.cb_notip);
if(ConstMethod.b_forceToUpdate){
cb_tip.setVisibility(View.GONE);
}
tv_content.setText(str_UpdateMessage);
btn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 取得欲安装程序之文件名称
fileEx = Account.url.substring(
Account.url.lastIndexOf(".") + 1, Account.url.length())
.toLowerCase();
fileNa = Account.url.substring(
Account.url.lastIndexOf("/") + 1,
Account.url.lastIndexOf("."));
Beginning();
myDialog.dismiss();
if (!ConstMethod.b_forceToUpdate&&cb_tip.isChecked()) {
share.edit()
.putString(ConstMethod.SHARE_VERSION, Account.ver)
.commit();
share.edit().putBoolean(ConstMethod.SHARE_NO_UPDATE, true)
.commit();
}
}
});
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
if (!ConstMethod.b_forceToUpdate&&cb_tip.isChecked()) {
share.edit()
.putString(ConstMethod.SHARE_VERSION, Account.ver)
.commit();
share.edit().putBoolean(ConstMethod.SHARE_NO_UPDATE, true)
.commit();
}
}
});
}
// 升级
protected Handler BroadcastHandler = new Handler() {
public void handleMessage(Message msg) {
updateAlertDialog();
}
};
protected void Beginning() {
LinearLayout ll = (LinearLayout) LayoutInflater.from(this)
.inflate(R.layout.layout_loadapk, null);
pb = (ProgressBar) ll.findViewById(R.id.down_pb);
tv = (TextView) ll.findViewById(R.id.tv);
Builder builder = new Builder(this);
builder.setView(ll);
builder.setTitle("版本更新进度提示");
builder.setNegativeButton("后台下载",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(NetworkActiviy.this,VersionService.class);
startService(intent);
dialog.dismiss();
}
});
builder.show();
new Thread() {
public void run() {
loadFile(Account.url);
}
}.start();
}
protected void loadFile(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
float length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment.getExternalStorageDirectory(),
fileNa + "." + fileEx);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
float count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
sendMsg(1, (int) (count * 100 / length));
}
}
sendMsg(2, 0);
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (Exception e) {
sendMsg(-1, 0);
}
}
protected void sendMsg(int flag, int c) {
Message msg = new Message();
msg.what = flag;
msg.arg1 = c;
handler.sendMessage(msg);
}
protected Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {// 定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case 1:
pb.setProgress(msg.arg1);
ConstMethod.loading_process = msg.arg1;
tv.setText("已为您加载了:" + ConstMethod.loading_process + "%");
break;
case 2:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), fileNa + "."
+ fileEx)),
"application/vnd.android.package-archive");
startActivity(intent);
pb.setVisibility(View.GONE);
break;
case -1:
String error = msg.getData().getString("error");
ToastInfo(error);
break;
}
}
super.handleMessage(msg);
}
};
//***************************************************************
//***************** 后台下载 ***************************
//***************************************************************
public class VersionService extends Service {
private NotificationManager notificationMrg;
private int old_process = 0;
private boolean isFirstStart=false;
public void onCreate() {
super.onCreate();isFirstStart=true;
notificationMrg = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// System.out.println(TestVersionUpdateActivity.loading_process+"==");
mHandler.handleMessage(new Message());
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 1为出现,2为隐藏
if(ConstMethod.loading_process>99){
notificationMrg.cancel(0);
stopSelf();
return;
}
if(ConstMethod.loading_process>old_process){
displayNotificationMessage(ConstMethod.loading_process);
}
new Thread() {
public void run() {
isFirstStart=false;
Message msg = mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
}.start();
old_process =ConstMethod.loading_process;
}
};
@Override
public void onDestroy() {
super.onDestroy();
}
private void displayNotificationMessage(int count) {
// Notification的Intent,即点击后转向的Activity
Intent notificationIntent1 = new Intent(this, this.getClass());
notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0,
notificationIntent1, 0);
// 创建Notifcation
Notification notification = new Notification(R.drawable.ic_launcher,
"XXX程序升级", System.currentTimeMillis());// 设定Notification出现时的声音,一般不建议自定义
if(isFirstStart || ConstMethod.loading_process>97){
notification.defaults |= Notification.DEFAULT_SOUND;// 设定是否振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
}notification.flags |= Notification.FLAG_ONGOING_EVENT;
// 创建RemoteViews用在Notification中
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.notification_version);
contentView.setTextViewText(R.id.n_title,
"升级提示");
contentView.setTextViewText(R.id.n_text, " 当前进度:"+count+"% ");
contentView.setProgressBar(R.id.n_progress, 100, count, false);
notification.contentView = contentView;
notification.contentIntent = contentIntent1;
notificationMrg.notify(0, notification);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}