网址 http://blog.csdn.net/linglongxin24/article/details/53874148
/**进度条通知构建**/ NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context); /**设置为一个正在进行的通知 作用是为了不能手动删除**/ builderProgress.setOngoing(true); /**设置小图标**/ builderProgress.setSmallIcon(R.mipmap.ic_launcher); /**新建通知自定义布局**/ mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification); /**进度条ProgressBar**/ mRemoteViews.setProgressBar(R.id.pb, 100, 0, false); /**提示信息的TextView**/ mRemoteViews.setTextViewText(R.id.tv_message, "下载中..."); /**操作按钮的Button**/ mRemoteViews.setTextViewText(R.id.bt, "暂停"); /**设置左侧小图标*/ mRemoteViews.setImageViewResource(R.id.iv, R.mipmap.ic_launcher); /**设置通过广播形式的PendingIntent 来控制下载和暂停**/ Intent intent = new Intent(DownLoadService.ACTION_UPDATE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context,REQUEST_CODE_BROADCAST, intent, 0); mRemoteViews.setOnClickPendingIntent(R.id.bt, pendingIntent);//布局中的点击事件 /**设置自定义布局**/ builderProgress.setContent(mRemoteViews); /**设置滚动提示**/ builderProgress.setTicker("开始下载..."); notification = builderProgress.build(); /**设置不可手动清除**/ notification.flags = Notification.FLAG_NO_CLEAR; /**获取通知管理器**/ notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); /**发送一个通知 参数一 是为了确定同一条消息,参数二 notification的对象**/ notificationManager.notify(NOTIFICATION_ID, notification);
public Builder setTicker(CharSequence tickerText)设置状态栏开始动画的文字
public Builder setContentTitle(CharSequence title)设置内容区的标题,必须设置
public Builder setContentText(CharSequence text)设置内容区的内容,必须设置
public Builder setContentIntent(PendingIntent intent)设置点击通知后操作(可以跳转Activity,打开Service,或者发送广播)
public Builder setColor(@ColorInt int argb)这个可以设置smallIcon的背景色
public Builder setSmallIcon(@DrawableRes int icon)设置小图标,必须设置
*public Builder setLargeIcon(Bitmap b) *设置打开通知栏后的大图标
*public Builder setWhen(long when) *设置显示通知的时间,不设置默认获取系统时间,这个值会在Notification上面显示出来
*public Builder setAutoCancel(boolean autoCancel) *设置为true,点击该条通知会自动删除,false时只能通过滑动来删除
public Builder setPriority(int pri)设置优先级,级别高的排在前面
public Builder setDefaults(int defaults)设置上述铃声,振动,闪烁用|分隔,常量在Notification里
public Builder setOngoing(boolean ongoing)设置是否为一个正在进行中的通知,这一类型的通知将无法删除
通知的提醒方式
- 声音提醒默认声音notification.defaults |= Notification.DEFAULT_SOUND;自定义声音notification.sound = Uri.parse("file:///sdcard0/notification.ogg");
- 震动提醒默认振动notification.defaults |= Notification.DEFAULT_VIBRATE;自定义振动long[] vibrate = {100, 200, 300, 400}; //震动效果,表示在100、200、300、400这些时间点交替启动和关闭震动notification.vibrate = vibrate;
- 闪烁提醒默认闪烁notification.defaults |= Notification.DEFAULT_LIGHTS;自定义闪烁notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志