/** 显示带进度条通知栏 */ public void showProgressNotify(final String apkurl) { mBuilder.setContentTitle("等待下载") .setContentText("进度:") .setTicker("开始下载");// 通知首次出现在通知栏,带上升动画效果的 mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条 设置为true就是不确定的那种进度条效果 mNotificationManager.notify(notifyId, mBuilder.build()); new Thread(){ @Override public void run() { super.run(); try { URL url = new URL(apkurl); URLConnection connection = url.openConnection(); connection.connect(); // this will be useful so that you can show a typical 0-100% progress bar int fileLength = connection.getContentLength(); // download the file InputStream input = new BufferedInputStream(connection.getInputStream()); File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); OutputStream output = new FileOutputStream(new File(file,"LXInstall.apk")); apkpath = file.getAbsolutePath()+"/"+"LXInstall.apk"; byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count; // publishing the progress... progress = (int) (total * 100 / fileLength); mBuilder.setContentTitle("下载中") .setContentText("进度:"+progress*100/100+"%"); mBuilder.setProgress(100, progress, false); // 这个方法是显示进度条 mNotificationManager.notify(notifyId, mBuilder.build()); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } mBuilder.setAutoCancel(true) .setContentTitle("下载完成") .setProgress(100, 100, false) .setContentText("点击安装!"); Intent apkIntent = new Intent(); apkIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); apkIntent.setAction(android.content.Intent.ACTION_VIEW); Uri uri = Uri.fromFile(new File(apkpath)); apkIntent.setDataAndType(uri, "application/vnd.android.package-archive"); PendingIntent contextIntent = PendingIntent.getActivity(MainActivity.this, 0,apkIntent, 0); mBuilder.setContentIntent(contextIntent); mNotificationManager.notify(notifyId, mBuilder.build()); } }.start(); }
下载更新App
最新推荐文章于 2024-01-19 13:28:32 发布