通知Notification版本兼容处理

来源:互联网 发布:淘宝我的发票灰色的 编辑:程序博客网 时间:2024/05/17 07:12

运行在后台的Service,需要某种方式来通知用户,例如通知用户来电,通知有新的消息。这类的通知显示在status bar上,还可以带有硬件的提醒,例如振动、LED灯闪,播放声音等等。在Android中,可以通过NotificationManager来发起一个通知。

我们先看一个简单的例子,如有图所示。界面很简单,两个大button,上面是出发通知,下面的是取消通知。触发通知后,状态栏显示图标和通知摘要(ticker,股市的自动收录机),几秒后只剩下图片,下拉状态栏,这以TextView的方式显示内容的标题title和文本。点击通知后,跳到某个activity中。

创建点击通知后出发的activity,采用简单方式,使用TextView方式

public class NotifyMessage extends Activity{ 
 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        TextView t = new TextView(this); 
        t..setText("This is the message!");
        setContentView(t);
    }

}

下面是通知的处理程序

public class NotifyTest extends Activity{
    private NotificationManager mgr = null;//通过通知管理器来实现通知操作
    int count = 0; //有时service会发出多条通知,我们用一个计数器来模拟不同的通知
    private static final int NOTIFY_ME_ID=1337; //通知管理器通过通知ID来标识不同的通知,创建和删除通知是需要提供通知号。
    
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notify_test);
 
        //步骤1:在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。 
        mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }
   
   /*按上面button,出发notifyMe( ) */
    public void notifyMe(View v){ 
        count ++; //这是通知的序列号,每按一次button,模拟一条新的通知
        //步骤2:创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条是出发的activity,所以采用的是PendingIntent
        PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this,NotifyMessage.class), 0);
        //步骤3:通过Notification.Builder来创建通知,注意这是在API Level 11之后才支持,如果要兼容Android 2.x的版本,可以看后面注释内的代码
        Notification myNotify
= new Notification.Builder(this)
                                .setSmallIcon(R.drawable.note) //设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap icon)
                                .setTicker("Ticker:"+count)//设置在status bar上显示的提示文字
                                .setContentTitle("Title:" + count)//设置在下拉status bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
                                .setContentText("Notification Text" + count)//TextView中显示的详细内容
                                .setContentIntent(i) //关联PendingIntent
                                .setNumber(count) //在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
                                .build(); //需要注意build()是在API level 16增加的,可以使用 getNotificatin()来替代
     /* 下面需兼容Android 2.x版本是的处理方式
       Notification myNotify=new Notification(R.drawable.note, "Ticker:" + count,  System.currentTimeMillis());
        myNotify.setLatestEventInfo(this, "Notification Title",  "This is the notification message", i);
        myNotify.number = count; */ 
         
       myNotify.flags |= Notification.FLAG_AUTO_CANCEL; //FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
        mgr.notify(NOTIFY_ME_ID ,myNotify);//步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
    }
   
    /* 按下面的button,触发消除通知 */
    public void notifyClear(View v){ 
        mgr.cancel( NOTIFY_ME_ID );
    }   

}

如果通知还需要硬件方面的提醒,例如振动,可如下处理

note.setVibrate(new long[] {500L, 200L, 200L, 500L})
//note.vibrate=new long[] {500L, 200L, 200L, 500L};

Notification.Builder提供了setLights( ),setSound( )等方法,还有其他很多等等,具体可以根据自己的需求在参考中查找。

0 0
原创粉丝点击