Android8.0通知栏配合异步线程下载

//用于发送通知,ID和Name可以修改,确保唯一性即可
    private static final int PUSH_NOTIFICATION_ID = (0x002);
    private static final String PUSH_CHANNEL_ID = "PUSH_NOTIFY_ID";
    private static final String PUSH_CHANNEL_NAME = "PUSH_NOTIFY_NAME";
  
  private void testAsyncTaskAndNotify() {
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //创建通知通道之前先判断sdk是否>=26,若是则需要创建channel才允许发送通知
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);//如果为IMPORTANCE_HIGH则会一直弹出提示及提示音
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }

        final Notification.Builder builder = new Notification.Builder(MainActivity.this);
        final Notification notification = builder.build();
        //下面通过创建一个匿名类执行异步任务,
        AsyncTask<Void, Integer, Boolean> task = new AsyncTask() {

            @Override
            protected void onProgressUpdate(Object[] values) {
                builder.setContentTitle("Download:");
                builder.setContentText(values[0].toString() + "%");
                builder.setSmallIcon(R.drawable.blue);
                builder.setWhen(System.currentTimeMillis());
                builder.setChannelId(PUSH_CHANNEL_ID);
                if (manager != null) {
                    manager.notify(PUSH_NOTIFICATION_ID, notification);
                }
                super.onProgressUpdate(values);
                if (values[0].equals(100)) {
                    manager.cancel(PUSH_NOTIFICATION_ID);
                    Toast.makeText(MainActivity.this, "download success!!!", Toast.LENGTH_SHORT).show();
                    onCancelled();
                }
            }

            @Nullable
            @Override
            protected Object doInBackground(Object[] objects) {
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(1000);//线程休眠1000ms
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    publishProgress(i);
                    if (isCancelled()) {
                        break;
                    }
                }
                return null;
            }
        };
        task.execute();//一定要记得execute,开启异步线程
    }

遇到的问题:

  1. 进度条一直弹出提示及提示音
    只需要把Notify的importance值从“IMPORTANCE_HIGH”改为“IMPORTANCE_LOW”即可。
    参考链接:https://blog.csdn.net/qq_25749749/article/details/80448313
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值