状态栏NotificationCompat

来源:互联网 发布:提花制版软件 编辑:程序博客网 时间:2024/05/16 03:17

NotificationCompat:显示系统的桌面通知栏上,经常用于消息通知等
创建通知

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);//创建对象Builder,通过调用builder的方法返回Notification对象builder.setContentTitle(标题);    //设置标题builder.setContentText(内容);  //设置内容builder.setSmallIcon(图标);    //设置图标Intent itt = new Intent(Intent.ACTION_MAIN);itt.addCategory(Intent.CATEGORY_LAUNCHER);itt.setComponent(new ComponentName(context.getPackageName(), context.getPackageName() + "." + ((Activity)        context).getLocalClassName()));itt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);// 关键的一步,设置启动模式PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, itt, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pendingIntent); //设置通知栏点击意图       builder .setTicker(测试) //通知首次出现在通知栏,带上升动画效果的      builder.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间     builder .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  builder .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消      builder  .setOngoing(false)//ture,设置他为一个正在进行的通知,以某种方式正在等待,占用设备(如一个文件下载,同步操作,主动网络连接)     builder .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON     builder .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果使用当前的用户默认设置//builder.setLights(Color.GREEN, 1500, 1000);   //设置通知三色灯(颜色,亮的时间,暗的时间 )颜色跟设备有关//builder.setSound(Uri.parse("file:///sdcard/xx/xx.mp3"))  //获取自定义铃声  //builder.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"))//获取Android多媒体库内的铃声  Notification nf = builder.build();nf.flags |= Notification.FLAG_NO_CLEAR;// 点击通知后自动消失notification.defaults |= Notification.DEFAULT_SOUND;// 点击通知后自动声音notification.defaults |= Notification.DEFAULT_VIBRATE;// 点击通知后自动震动NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);manager.notify(0, nf);  //唤醒通知,第一个参数表示该通知在应用全局的唯一标识

提醒标志符成员:

Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)Notification.FLAG_INSISTENT   //让声音、振动无限循环,直到用户响应 (取消或者打开)Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失Notification.FLAG_NO_CLEAR          //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务

setDefaults(int defaults)对应属性:

Notification.DEFAULT_VIBRATE    //添加默认震动提醒  需要 VIBRATE permissionNotification.DEFAULT_SOUND    // 添加默认声音提醒Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒Notification.DEFAULT_ALL// 添加默认以上3种全部提醒

该问借鉴:http://blog.csdn.net/vipzjyno1/article/details/25248021/

原创粉丝点击