封装好的Notification 方法 Dome 拿去用
/** * Noification 通知栏 */ public class mNotification { public Context mcontext; public String title;//标题 public String text;//内容 public String ticker;//上浮语句 private Class<?> cls;//跳转的界面.class public void In(Context context,Class<?> cls,String title,String text, String ticker){ this.mcontext=context; this.title=title; this.text=text; this.ticker=ticker; this.cls=cls; Notification (); } public void Notification() { //获取状态栏管理 NotificationManager notificationManager=(NotificationManager) mcontext.getSystemService ( NOTIFICATION_SERVICE); //实例化通知栏构造器NotificationCompat.builder NotificationCompat.Builder builder=new NotificationCompat.Builder ( mcontext ); //对builder进行配置 builder.setContentTitle ( title)//title .setContentText (text )//测试内容 .setContentIntent(getDefalutIntent( Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图 // .setNumber(number) //设置通知集合的数量 .setTicker(ticker) //通知首次出现在通知栏,带上升动画效果的 .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级 // .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消 .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合 //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON Notification notification = builder.build(); notification.flags = Notification.FLAG_ONGOING_EVENT ; notification.flags = Notification.FLAG_NO_CLEAR;//点击清除的时候不清除 Intent intent = new Intent(mcontext.getApplicationContext(),cls); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); PendingIntent pendingIntent = PendingIntent.getActivity(mcontext.getApplicationContext(), 0, intent, 0); builder.setContentIntent(pendingIntent); notificationManager.notify(0, builder.build ()); } public PendingIntent getDefalutIntent(int flags){ PendingIntent pendingIntent= PendingIntent.getActivity(mcontext ,1, new Intent(), flags); return pendingIntent; } }