码迷,mamicode.com
首页 > 移动开发 > 详细

android Notification

时间:2016-05-08 15:15:40      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

 

发送通知可分为3步:

1、获取NotificationManager:

 NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2、创建Notification:

        Intent intent=new Intent(this,SecActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("title")
                .setTicker("ticker")
                .setContentText("message")
                .setContentIntent(contentIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .build();

3、NotificationManager发送:

notificationManager.notify(1, notification1);

发送通知时的声音、振动和呼吸灯控制:

在不进行设置的情况下,没有任何提示效果,为了提醒用户,设置提示效果很有必要。

通常我们会使用系统默认的效果(使用的这种方法后,将会对其它的效果设置进行覆盖):

notification.defaults = Notification.DEFAULT_ALL;

 

我们也可以自定义设置提示效果:

1、声音提示:

(1)使用默认提示音:

notification.defaults |= Notification.DEFAULT_SOUND;

(2)自定义提示音:

notification.sound = Uri.parse("……/name.mp3");

2、

(1)使用默认振动效果:

notification.defaults |= Notification.DEFAULT_VIBRATE;

(2)自定义振动效果:

 notification.vibrate=new long[]{0,200,800,200,800,200};

数组第1个为等待时间,第2个为振动时间,数组第3个为等待时间,第4个为振动时间……以此类推。

3、

(1)使用默认呼吸灯效果:

notification.defaults |= Notification.DEFAULT_LIGHTS;

(2)自定义呼吸灯效果:

        notification.ledARGB = 0x00ff00;
        notification.ledOnMS = 300;
        notification.ledOffMS = 300;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

上面三个分别设置呼吸灯颜色、明暗时间。


上文自定义呼吸灯使用了notification.flags进行设置,除此之外,

notification.flags还可以设置通知的许多特性,下面说说几个常用的:

1、FLAG_AUTO_CANCEL
点击通知后,这个通知就会自动取消掉。
2、FLAG_INSISTENT
通知音频将会不断重复播放,直到用户对这个通知作出响应。
3、FLAG_ONGOING_EVENT
这个标志可以将这个 notification 归类到“正在运行”下。这表示应用仍在运行—也就是说它的进程仍在后台运行。
4、FLAG_NO_CLEAR flag
用户点击清除通知键,通知不会被清除。

 


 

自定义通知UI:

系统默认的UI布局很多时候无法满足我们的需求,于是我们得自定义一个布局,这里需要用到RemoteViews: 

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
        contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
        contentView.setTextViewText(R.id.title, "title");
        contentView.setTextViewText(R.id.text, "text");
        Intent intent = new Intent(this,SecActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentIntent(contentIntent)
                .setContent(contentView)
                .setSmallIcon(R.mipmap.ic_launcher)
                .build();
        notificationManager.notify(3, notification);

RemoteViews还可以用setOnClickPendingIntent方法实现点击事件
用setProgressBar方法更新通知栏的进度条状态……还有很多更新UI的方法,这些就不详细说明了。



PendingIntent: 

PendingIntent与Intent类似,都是用于实现某种意图,PendingIntent可看做Intent的包装类。两者的差别是Intent的目的都是立即执行,PendingIntent则是延迟执行。

 

android Notification

标签:

原文地址:http://www.cnblogs.com/zhisuoyu/p/5470508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!