消息栏通知的实现:
方法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); // 通过通知管理器发送通知