下载更新App

/** 显示带进度条通知栏 */
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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值