手机自带的下载状态栏可能觉得有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);