Android自定义下载状态栏

手机自带的下载状态栏可能觉得有low,那就试试自定义的view吧。
思路:通过RemoteViews获取布局,并设置标题等,NotificationCompat.Builder对象加载布局。

主要代码(下面用到的布局是笔者自己的布局R.layout.download):

private NotificationCompat.Builder ncb = null;
private Notification notification;
//创建
nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//这里需要设置图片(下载过程中又不会显示),不设置又可能就没法显示自定义的下载布局(亲测)。不知道原因
ncb = (NotificationCompat.Builder) new NotificationCompat.Builder(getBaseContext())
    .setSmallIcon(R.mipmap.ic_launcher);
//RemoteViews 获取自定义布局
 RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.download);
//设置布局中TextView属性,前面的id是布局中TextView的id,后面是要设置的值,布局中的其它空间均有类似的方法
contentView.setTextViewText(R.id.bar_name,"下载进度");
//加载布局
ncb.setContent(contentView);
//创建状态栏
notification = ncb.build();
//通知更新状态栏
nm.notify(0, notification);
//下载过程中更新状态栏
RemoteViews contentView = notification.contentView;
          contentView.setProgressBar(R.id.bar,100,values[0],false);
//设置布局中的监听,有两个监听方式:setOnClickFillInIntent(),setOnClickPendingIntent()方法中需要传入intent和pendingIntent参数,前面的id是布局中控件的id
 contentView.setOnClickPendingIntent(R.id.bar,pendingIntent);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值