您的位置:首页 > 其它

Nofication的使用

2016-05-15 15:30 295 查看
//获取NotificationManager对象

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)//

.setSmallIcon(R.drawable.ic_launcher)//设置显示的小图标

.setContentTitle("标题")//设置显示的标题

.setContentText("这是内容")//设置显示的内容

.setWhen(System.currentTimeMillis())//设置到达的时间

.setTicker("消息来了...");//设置第一次到达显示提示信息

Notification notification = builder.build();

//pendingIntent对象是个跳转Intent当用户点击通知提示栏时打开一个Activity

PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(MainActivity.this,MainActivity.class),0);

//设定下拉通知栏时显示的标题及内容信息

notification.setLatestEvenInfo(getApplicationContext(),"通知标题","这是个新的通知",pendingIntent);

//设定通知当用户单击后自动消失

notification.flags|=Notification.FLAG_AUTO_CANCEL;

//设定通知触发时的默认声音

notification.defaults|=Notification.DEFAULT_SOUND;

//通过管理器的notify方法发出通知

manager.notify(1, notification);

Notification的另一种创建方法

Notification notification = new Notification(R.drawable.icon,"我的通知",System.getcurrentTimeMillis());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: