版本升級實現代碼 點擊下載安裝 並在通知欄顯示,下載安裝通知
private void loadNewApp() { String loadurl = "http://.........";//檔案的網路路徑 final String store = "/sdcard" + "/apkname" + new Random(127) + UserUtils.getRoundChar(6) + ".apk";//下載的檔案儲存體位置 HttpUtils http = new HttpUtils();//使用xutils下載檔案 http.download(loadurl, store , true, new RequestCallBack<File>() { @Override public void onSuccess( ResponseInfo<File> arg0) { //取消通知 mNotificationManager.cancel(1); //開啟安裝介面 Intent intent = new Intent(Intent.ACTION_VIEW); File apkfile = new File(store); intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); activity.startActivity(intent); } @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); long process = current / total; //開啟通知 setUpNotification((int) process, (int) total); } @Override public void onFailure(HttpException arg0, String arg1) { rl_loading.setVisibility(View.INVISIBLE); } }); }<pre name="code" class="java"> private NotificationManager mNotificationManager; private void setUpNotification(int process, int max) { mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.ic_launcher; CharSequence tickerText = "...開始下載"; long when = System.currentTimeMillis(); Notification mNotification = new Notification(icon, tickerText, when); mNotification.flags = Notification.FLAG_ONGOING_EVENT; RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.download_notification_layout); contentView.setTextViewText(R.id.name, "...正在下載..."); contentView.setProgressBar(R.id.progressbar, max, process, true); mNotification.contentView = contentView;//設定點擊通知欄條目 跳轉的activity// Intent intent = new Intent(activity, NewDianPuActivity.class);// PendingIntent contentIntent = PendingIntent.getActivity(activity, 0, intent,// PendingIntent.FLAG_UPDATE_CURRENT); // mNotification.contentIntent = contentIntent; mNotificationManager.notify(1, mNotification); }