android通知栏总结

通知消息的发送
12-1:消息管理者
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
12-2:下面是点击通知之后的操作,即进行另一个页面
intent.setClass(NoticeActivity.this, NotificationActivity.class);
12-3:消息意图(可延迟的)
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
12-4:新建消息
Notification notification = new Notification(R.drawable.ic_launcher,"内容",System.currentTimeMillis());
12-5:设置消息的内容
notification.setLatestEventInfo(NoticeActivity.this, "还要什么标题啊。。", "嗯", pi);
12-6:设置它的可取消性与否
可滑动取消:
notify.flags=Notification.FLAG_AUTO_CANCEL;
不可滑动取消:
notify.flags=Notification.FLAG_NO_CLEAR
12.7:设置声音
系统自带的声音:
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
默认的声音:
notify.defaults=Notification.DEFAULT_SOUND;
自定义声音
notify.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
当用户响应后声音和会结束:
notification.flags |= notification.FLAG_INSISTENT;
12-8:添加振动
默认:
notification.defaults |= Notification.DEFAULT_VIBRATE;
当然也可以自己定义振动形式,这边需要用到Long型数组
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
权限:
<uses-permission android:name="android.permission.VIBRATE"/>
12-9:添加灯光
默认的灯光:
notification.defaults |= Notification.DEFAULT_LIGHTS;
自定义:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。
注意:这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。
12-10:开始通知
manager.notify(1,notification);
12-11:方法二:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker("有您的新通知")
.setWhen(System.currentTimeMillis())
.setContentTitle("这是标题")
.setContentText("这是内容")
.setContentInfo("AA")//设置右下角的内容
.setContentIntent(pi)
.setNumber(1)
.setAutoCancel(true);
Notification notif = builder.build();
manager.notify(2,notif);
12-12:自定义:
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
PendingIntent pi2 = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);

RemoteViews rv = new RemoteViews(getPackageName(),R.layout.music_notify);
rv.setOnClickPendingIntent(R.id.iv_icon_notify, pi);
rv.setOnClickPendingIntent(R.id.tv_text_notify, pi2);
rv.setTextViewText(R.id.tv_text_notify, "这这这这这这。。。。。");

NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setSmallIcon(R.drawable.ic_launcher).setTicker("有您的通知").setWhen(System.currentTimeMillis()).setContent(rv);
Notification n = b.build();
manager.notify(3,n);

上一篇:Xilinx 网站资源导


下一篇:URLOS开发基础教程——docker容器的使用方法