下载apk,并将进度显示在通知栏

public class MyDownloadAnsy extends AsyncTask<String, Integer, Integer> {


    private File file;
    private NotificationManager nm;
    private Notification nf;
    private NotificationCompat.Builder builder;

    public MyDownloadAnsy() {
        nm = (NotificationManager) App.application.getSystemService(Activity.NOTIFICATION_SERVICE);
        builder = new NotificationCompat.Builder(App.application)
                .setSmallIcon(R.mipmap.ic_launcher).setContentInfo("下载中...").setContentTitle("正在下载");
        nf = builder.build();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Integer doInBackground(String... params) {
        File sdDir = Environment.getExternalStorageDirectory();
        File file = new File(sdDir, SystemClock
                .currentThreadTimeMillis() + ".apk");
        file = new File(sdDir, "www.sodasoccer.apk");
        if (file.exists()) {
            file.delete();
            file = new File(sdDir, "www.sodasoccer.apk");
        }
        HttpURLConnection con = null;
        InputStream is = null;
        OutputStream os = null;
        int statue = 1;
        try {
            URL url = new URL(params[0]);
            con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(5 * 1000);  //设置超时时间
            if (con.getResponseCode() == 200) { //判断是否连接成功
                int fileLength = con.getContentLength();
                is = con.getInputStream();    //获取输入
                os = new FileOutputStream(file);
                byte[] buffer = new byte[1024 * 1024 * 10];
                long total = 0;
                int count;
                int pro1 = 0;
                int pro2 = 0;
                while ((count = is.read(buffer)) != -1) {
                    total += count;
                    if (fileLength > 0)
                        pro1 = (int) (total * 100 / fileLength);  //传递进度(注意顺序)
                    if (pro1 != pro2)
                        publishProgress(pro2 = pro1);
                    os.write(buffer, 0, count);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            statue = 2;
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (con != null) {
                con.disconnect();
            }
        }
        return statue;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        if (result == 1) {
            Toast.makeText(App.application, "下载完成", Toast.LENGTH_SHORT).show();
        } else if (result == 2) {
            Toast.makeText(App.application, "下载失败,请点击应用管理打开存储权限", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        Log.d("===", "" + values[0]);
        super.onProgressUpdate(values);
        builder.setProgress(100, values[0], false);
        nf = builder.build();
//        int i = new Random().nextInt();
        nm.notify(0, nf);
        if (values[0] == 100) {    //下载完成后点击安装
            Intent it = new Intent(Intent.ACTION_VIEW);
            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            it.setDataAndType(Uri.parse("file://" + file.getPath()), "application/vnd.android.package-archive");
            PendingIntent pendingIntent = PendingIntent.getActivity(App.application, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
            builder
                    .setContentTitle("下载完成")
                    .setContentText("点击安装")
                    .setContentInfo("")
                    .setContentIntent(pendingIntent);
            nf = builder.build();
            nm.notify(0, nf);
        }
    }

}

另外遇到的问题:

1.当我下载完成,但是我并不安装,给通知栏隐藏掉,下次我再点击更新的时候是不会弹出的,

2.当使用同一个路径发送网络请求,不知道AsyckTask是不是会缓存这个请求,还是我file都是相同的原因,刚一点击,

就提示下载完成,这个有待确认

解决办法:

1.如果要让notification更新,需要

new NotificationCompat.Builder().build,重新构建,并重新nt.notify,重新通知,于是我把他放在了构造方法里,
每次创建任务,都重新构建一个,另nitification,以id为标识.
2.发送的url请求后面,加上了当前时间的毫秒值,保证每次发送的请求都不重复,如果已下载的文件,删除重新创建
ps:让AsyncTask执行:  new MyDownloadAnsy().excul(url);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值