android 通知

2017-05-25  本文已影响12人  EvanPoison
  1. notification需要一个NotificationManager来管理,如何获取呢?
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  2. 然后使用builder构造器来构造一个Notification对象,为了兼容性,最好使用v4包中的NotificationCompat
 Notification notification =  new NotificationCompat.Builder(this)
                .setContentTitle("hello")
                .setContentText("test")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon()
                .setLargeIcon()
                .build();
  1. 最后调用NotificationManager的notify()方法
  2. 以上步骤创建的通知是无法点击的,需要设置PendingIntent
    PendingIntent有几个静态方法用来获取对象
PendingIntent.getActivities()
PendingIntent.getBroadcast()
PendingIntent.getService()

NotificationCompat.Builder里有个方法:setContentIntent()来设置这个延迟的意图

5.通知的关闭

  1. 更高级的通知,待续。。。
上一篇 下一篇

猜你喜欢

热点阅读