天天看点

Notifaction安卓通知栏二。详细讲解三。使用Notifaction.Build四。N常量五。flag

因为现在商业程序,多数使用Notifaction进行信息推送。同时也有程序进行android程序的后台前挂。。这里把Notifaction放在Service这个归类里面,进行总结和学习。

目的:Broadcast Receiver组件并没有提供可视化界面来显示广播信息,而Notifaction和NotifactionManager可以实现可视化的信息显示,通过它们可以显示广播信息的内容以及图标和震动等信息(在状态栏上)。

一。具体步骤:

1)获得系统级的服务Notification Manager,调用getSystemService()方法实现

  1. String service = NOTIFICATION_SERVICE;  
  2. nm=(NotificationManager)getSystemService(service); 
  1. //实例化Notification  
  2.        n = new Notification();  
  3.        //设置显示图标,该图标会在状态状态栏显示  
  4.        int icon = n.icon=R.drawable.notification;  
  5.        //设置显示提示信息,该信息会在状态栏显示  
  6.        String tickerText="Test Notification";  
  7.        //显示时间  
  8.        long when = System.currentTimeMillis();  
  9.        n.icon=icon;  
  10.        n.tickerText=tickerText;  
  11.        n.when=when;
  1. Intent intent = new Intent(MainActivity.this,MainActivity.class);  
  2.                 //获取pendingIntent  
  3.                 PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);  
  4.                 //设置事件信息  
  5.                 n.setLatestEventInfo(MainActivity.this, "My Title", "My Content", pi);  
  6.                 //发出通知  
  7.                 nm.notify(ID,n);  

以上,就实现了一个简单的通知栏,可以通过nm.cancel(id-int)消除。

二。详细讲解

1.实例化一个通知,指定了图标、概要、时间  

Notification notification = new Notification(android.R.drawable.stat_notify_chat, tickerText, System.currentTimeMillis());

Notifaction安卓通知栏二。详细讲解三。使用Notifaction.Build四。N常量五。flag

2.指定通知的标题、内容和intent  

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:104040444"));  

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);  

notification.setLatestEventInfo(this, title, content, pendingIntent);  

3.指定标志和声音  

notification.flags = Notification.FLAG_AUTO_CANCEL;  

notification.defaults = Notification.DEFAULT_SOUND;  

4.可以指定为震动,也可以用 .sound来自己指定声音  (sound是一个Uri对象.如何指定Raw文件可以见博文:http://blog.csdn.net/java2009cgh/article/details/7549679)

//notification.defaults = Notification.DEFAULT_VIBRATE;  

5.可以指定为闪光灯   //notification.defaults = Notification.DEFAULT_LIGHTS;  

Notification.build构造Notification方法介绍:   

三。使用Notifaction.Build

    Notification.build构造Notification方法介绍:  void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence  contentText,PendingIntent contentIntent)  

  功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象

     参数:    context              上下文环境

                  contentTitle       状态栏中的大标题

           contentText       状态栏中的小标题

   contentIntent    点击后将发送PendingIntent对象

      说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!

四。N常量

下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动 
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;  
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)      

五。flag

//设置flag位
FLAG_AUTO_CANCEL  该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR     该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应