欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Notification简介(转)

程序员文章站 2022-07-13 14:32:37
...

Notification看名字就知道,是一个和提醒有关的东西,它通常NotificationManager一块使用。具体来说,其主要功能如下。

  1.NotificationManager和Notification用来设置通知

  通知的设置等操作相对比较简单,基本的使用方式就是新建一个Notification对象,设置好通知的各项参数,然后使用系统后台运行的NotificationManager服务将通知发出来。基本步骤如下。

  1)创建NotificationManager对象,NotificationManager(通知管理器):负责通知用户事件的发生. 

NotificationManager有三个公共方法: 

1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走. 

2. cancelAll() 取消以前显示的所有通知. 

3. notify(int id,  Notification notification) 把通知持久的发送到状态条上.

代码如下:

      String ns = Context.NOTIFICATION_SERVICE;

  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

  
    2)创建一个新的Notification对象,Notification代表着一个通知。代码如下。

      Notification notification = new Notification();

  notification.icon = R.drawable.notification_icon;

  
    也可以使用稍微复杂一些的方式创建Notification,代码如下。

      int icon = R.drawable.notification_icon; //通知图标

  CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示

  long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

  Notification notification = new Notification(icon, tickerText, when);

  
    3)填充Notification的各个属性,代码如下。

      Context context = getApplicationContext();

  CharSequence contentTitle = "My notification";

  CharSequence contentText = "Hello World!";

  Intent notificationIntent = new Intent(this, MyClass.class);

  PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

  notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


  Notification提供了如下几种手机提示方式。
  状态栏(Status Bar)显示的通知文本提示,例如:

notification.tickerText = "hello";


  发出提示音,例如:

      notification.defaults = Notification.DEFAULT_SOUND;

  notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

  notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

  
    手机振动,例如:

notification.defaults = Notification.DEFAULT_VIBRATE;

  long[] vibrate = {0,100,200,300};

  notification.vibrate = vibrate;

  
    LED灯闪烁,例如:

      notification.defaults = Notification.DEFAULT_LIGHTS;

  notification.ledARGB = 0xff00ff00;

  notification.ledOnMS = 300;

  notification.ledOffMS = 1000;

  notification.flags |= Notification.FLAG_SHOW_LIGHTS;

  
    4)发送通知,代码如下。

      private static final int ID_NOTIFICATION = 1;

  mNotificationManager.notify(ID_NOTIFICATION, notification);

  
    2.更新通知

  如果需要更新一个通知,只需要在设置好Notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。

  为了更新一个已经触发过的Notification,传入相同的ID。用户既可以传入相同的Notification对象,也可以是一个全新的对象。只要ID相同,新的Notification对象会替换状态条图标和扩展的状态窗口的细节。

  另外,还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,代码如下。

notificationManager.cancel(notificationRef);


  当取消一个Notification时,会移除它的状态条图标以及清除在扩展的状态窗口中的信息。

 

    范例:

 

view plaincopy to clipboard
package cn.com.chenzheng_java;  
  
import android.app.Activity;  
import android.app.Notification;  
import android.app.NotificationManager;  
import android.app.PendingIntent;  
import android.content.Context;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.provider.MediaStore.Audio;  
import android.view.View;  
import android.widget.Button;  
  
/*** 
 * @description 状态栏通知相关 
 * @author chenzheng_java 
 *  
 */  
public class NotificationActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.notification);  
  
        Button button = (Button) findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                addNotificaction();  
  
            }  
        });  
  
    }  
      
      
      
  
    /** 
     * 添加一个notification 
     */  
    private void addNotificaction() {  
        NotificationManager manager = (NotificationManager) this  
        .getSystemService(Context.NOTIFICATION_SERVICE);  
        // 创建一个Notification   
        Notification notification = new Notification();  
        // 设置显示在手机最上边的状态栏的图标   
        notification.icon = R.drawable.excel;  
        // 当当前的notification被放到状态栏上的时候,提示内容   
        notification.tickerText = "注意了,我被扔到状态栏了";  
          
        /*** 
         * notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发 
         * notification.contentView:我们可以不在状态栏放图标而是放一个view 
         * notification.deleteIntent 当当前notification被移除时执行的intent 
         * notification.vibrate 当手机震动时,震动周期设置 
         */  
        // 添加声音提示   
        notification.defaults=Notification.DEFAULT_SOUND;  
        // audioStreamType的值必须AudioManager中的值,代表着响铃的模式   
        notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
          
        //下边的两个方式可以添加音乐   
        //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");    
        //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");    
        Intent intent = new Intent(this, Notification2Activity.class);  
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
        // 点击状态栏的图标出现的提示信息设置   
        notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);  
        manager.notify(1, notification);  
          
    }  
  
}  

 

转自:http://tech.it168.com/a2012/0106/1298/000001298952_1.shtml

 http://blog.csdn.net/jdsjlzx/article/details/6702537

 

相关标签: Notification