Notification的一些常用内容~

下面主要介绍这三个类:

一、NotificationManager

这个类是这三个类中最简单的。主要负责将Notification在状态显示出来和取消。主要包括5个函数:void cancel(int id),void cancel(String tag, int id),void cancelAll(),void notify(int id, Notification notification),notify(String tag, int id, Notification notification)

看看这五个函数就知道这个类的作用了。但是在初始化对象的时候要注意:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

二、Notification

设置这个类主要是设置Notification的相关属性。初始化

Notification n = new Notification();

Notification里面有很多属性下面选择几个常用的介绍一下

icon 这个是设置通知的图标。像QQ的小企鹅

sound 这个是设置来通知时的提示音。

tickerText 设置提示的文字。

vibrate 来通知时振动。

when 设置来通知时的时间

flag 这个很有意思是设置通知在状态栏显示的方式。它的值可以设置为虾米这些值:

FLAG_NO_CLEAR 将flag设置为这个属性那么通知栏的那个清楚按钮就不会出现

FLAG_ONGOING_EVENT 将flag设置为这个属性那么通知就会像QQ一样一直在状态栏显示

DEFAULT_ALL 将所有属性设置为默认

DEFAULT_SOUND 将提示声音设置为默认

DEFAULT_VIBRATE 将震动设置为默认

notification的属性表示的是在收到了之后的提示方式:
notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发
notification.contentView:我们可以不在状态栏放图标而是放一个view
notification.deleteIntent 当当前notification被移除时执行的intent
notification.vibrate 当手机震动时,震动周期设置
notification.defaults=Notification.DEFAULT_SOUND; // 添加声音提示
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;// audioStreamType的值必须AudioManager中的值,代表着响铃的模式


代码示例及说明

NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis()); 
n.flags = Notification.FLAG_AUTO_CANCEL;



Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); 
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(), 
R.string.app_name, 
i, 
PendingIntent.FLAG_UPDATE_CURRENT);

n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!", 
"Hello,there,I'm john.", 
contentIntent);
nm.notify(R.string.app_name, n);

第二个例子:

//获取NotificationManager实例
        String service = NOTIFICATION_SERVICE;
        nm = (NotificationManager)this.getSystemService(service);
       
        //实例化Notification
        n = new Notification();
        //设置显示图标,该图标会在状态栏显示
        int icon = R.drawable.icon;
        //设置显示提示信息,该信息也会在状态栏显示
        String tickerText = "Test Notifaction";
        //显示时间
        long when = System.currentTimeMillis();
       
        n.icon = icon;
        n.tickerText = tickerText;
        n.when = when;
        n.flags = Notification.FLAG_NO_CLEAR;
        n.flags = Notification.FLAG_ONGOING_EVENT;


 //实例化Intent
   Intent intent = new Intent(Main.this, Main.class);
   //获取PendingIntent
   PendingIntent pi = PendingIntent.getActivity(Main.this, 0, intent, 0);
   //设置事件信息
   n.setLatestEventInfo(Main.this, "My Title", "My Content", pi);
   //发出通知
   nm.notify(ID, n);
       

转载于:https://www.cnblogs.com/Tammie/archive/2012/08/25/2655801.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值