android 通知

  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.通知的关闭

  • 方式一:NotificationCompat.Builder中再加上setAutoCancel(),当通知被点击后,消失
  • 方式二:显式的调用NotificationManager的cancel()方法
  1. 更高级的通知,待续。。。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容