Notification消息栏通知的实现
来源:互联网 发布:mac电脑还原出厂设置 编辑:程序博客网 时间:2024/06/07 15:57
消息栏通知的实现:
方法1:直接new Notification()
NotificationManager manager=(NotificationManager)this.getSystemService(this.NOTIFICATION_SERVICE);
Notification notification = newNotification(icon, tickerText, when);//参数是消息栏通知的图片,弹通知时的标题,显示的时间
notification.flags |=Notification.FLAG_AUTO_CANCEL; // 在通知栏上点击此通知后自动清除此通知
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.sound = Uri .parse("android.resource://cn.otrue.patienthealthmanager/" + R.raw.tips);//使用自定义的铃声
notification.setLatestEventInfo(context,contentTitle, contentText,contentIntent);
int n=0;//可以为任意数字
mNotificationManager.notify(n, notification);
效果的实现
二、有效果的通知(震动、声音、亮灯)
这些功能的实现都是在通知之前进行设置
(1)震动
notification.defaults |=Notification.DEFAULT_VIBRATE;
还需要添加使用权限:<uses-permissionandroid:name="android.permission.VIBRATE"></uses-permission>
(2)声音
notification.defaults|=Notification.DEFAULT_VIBRATE;
(3)亮灯
三、显示效果---点击通知栏
(1)点击后消失
notification.flags = Notification.FLAG_AUTO_CANCEL;
(2)点击后无法销毁
notification.flags =Notification.FLAG_NO_CLEAR;
方法2:通过builder获取notification
NotificationManagermanager=(NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder1 = newNotificationCompat.Builder(MainActivity.this);
//也可以是Notification.Builderbuilder1 = new Notification.Builder(MainActivity.this);
builder1.setSmallIcon(R.drawable.ic_launcher);//设置图标
builder1.setTicker("显示第二个通知");//消息栏弹出来时显示的内容
builder1.setContentTitle("通知"); //在下拉通知栏中查看通知时,显示的通知的标题
builder1.setContentText(content.getText().toString());//在下拉植栏中查看通知时,显示的通知的内容
builder1.setWhen(System.currentTimeMillis());//发送时间
builder1.setDefaults(Notification.DEFAULT_ALL);//设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//点击后是否消失
Intent intent =new Intent(MainActivity.this,TwoActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,Integer.parseInt(num.getText().toString()), intent, flags);
builder1.setContentIntent(pendingIntent);
Notification notification1 =builder1.build();
manager.notify(124, notification1); // 通过通知管理器发送通知
自定义布局的消息栏通知
NotificationManagermanager=(NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
NotificationCompat.Builderbuilder1 = new NotificationCompat.Builder(MainActivity.this);
builder1.setSmallIcon(R.drawable.ic_launcher);//设置图标
builder1.setTicker("显示第二个通知");//消息栏弹出来时显示的内容
builder1.setContentTitle("通知"); //在下拉通知栏中查看通知时,显示的通知的标题
builder1.setContentText(content.getText().toString());//在下拉植栏中查看通知时,显示的通知的内容
builder1.setWhen(System.currentTimeMillis());//发送时间
builder1.setDefaults(Notification.DEFAULT_ALL);//设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//点击后是否消失
RemoteViews view = newRemoteViews(getPackageName(), R.layout.button_play);
PendingIntent intent1 =PendingIntent.getActivity(this, Integer.parseInt(num.getText().toString()), newIntent(this,ThreeActivity.class).putExtra("send", "开始"), 0);//自定义布局的意图的flag不能相同,相同则只会以最新的为标准//设置未知意图监听
view.setOnClickPendingIntent(R.id.play,intent1);//设置自定义布局上的按钮的未知意图的点击事件
PendingIntent intent2 =PendingIntent.getActivity(this, Integer.parseInt(num.getText().toString()), newIntent(this,ThreeActivity.class).putExtra("send", "停止"), 1);//自定义布局的意图的flag不能相同,相同则只会以最新的为标准
view.setOnClickPendingIntent(R.id.stop,intent2);//设置位置意图上的自定义布局上的控件的监听
Intent intent =new Intent(MainActivity.this,TwoActivity.class);
PendingIntent pendingIntent=null;
pendingIntent=PendingIntent.getActivity(MainActivity.this,Integer.parseInt(num.getText().toString()), intent, flags);
builder1.setContentIntent(pendingIntent);
Notification notification1 =builder1.build();
notification1.contentView = view;
manager.notify(Integer.parseInt(num.getText().toString()),notification1); // 通过通知管理器发送通知- Notification消息通知 通知栏的实现
- Notification消息栏通知的实现
- Notification实现消息通知
- android通知栏的消息通知(Notification)
- 通知栏通知:Notification的实现
- Android的消息通知--Notification
- Android的消息通知--Notification
- Notification.Builder实现发送一个普通的消息通知
- 消息栏通知(Notification)介绍
- 消息栏通知(Notification)介绍
- 消息栏通知(Notification)介绍
- 通知栏消息提醒--notification
- 消息栏通知(Notification)介绍
- Android Notification通知栏消息
- 通知栏消息通知详解(Notification)
- Android开发笔记之notification消息推送 通知栏的实现
- Notification(通知栏)的实现
- Android通知栏Notification的实现
- 第8周项目2 - 用对象数组操作长方形类
- JSP中的EL表达式(3)
- git开源库——性能优化——图片缓存
- angularjs中watch使用--实现项目中时时搜索
- 数据中心解决方案之灾备方案设计(下)
- Notification消息栏通知的实现
- 探讨kafka的分区数与多线程消费
- Halcon清晰度检测实例(转)
- UVA 524 Prime Ring Problem
- iOS AVCaptureSession获取图像数据
- 使用pushlet实现消息服务端推送至客户端
- jQuery学习笔记三:获取内容(text、html、val)
- Python使用xslt提取网页数据
- “应用程序无响应”原因汇总