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