生活随笔
收集整理的這篇文章主要介紹了
升级提示及加载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//***************************************************************
//*****************??? 啟動類?????????????? ***************************
//***************************************************************
//升級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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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;}};@Overridepublic void onDestroy() {super.onDestroy();}private void displayNotificationMessage(int count) {// Notification的Intent,即點擊后轉向的ActivityIntent notificationIntent1 = new Intent(this, this.getClass());notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0,notificationIntent1, 0);// 創建NotifcationNotification 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);}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}
}
?
總結
以上是生活随笔為你收集整理的升级提示及加载的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。