zoukankan      html  css  js  c++  java
  • Android笔记:通知

    可以在活动里创建,也可以在广播接收器里创建,还可以在服务里创建。

    NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    创建一个Notification 对象就可以写成:
    Notification notification = new Notification(R.drawable.icon, "——————This is ticker text——————",System.currentTimeMillis());

                          图标                    刚出的时候一闪而过             创建的时间

    notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);

                         标题         正文

    manager.notify(1, notification);//数字表示ID,每个消息的ID应不同。

    PendingIntent主要提供了几个静态方法用于获取PendingIntent的实例,可以根据需求来选择是使用getActivity()方法、getBroadcast()方法、还是getService()方法。

    第一个参数是Context。第二个参数传入0 即可。第三个参数是一个Intent 对象,可以通过这个对象构建出PendingIntent 的“意图”。

    第四个参数用于确定PendingIntent 的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT 和FLAG_UPDATE_CURRENT 这四种值可选。

    Intent intent = new Intent(this, NotificationActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
    notification.setLatestEventInfo(this, "This is content title","This is content text", pi);//第四个参数是调用pendingintent的。

    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(1);//取消通知 “1"就是一开始传入的ID。

  • 相关阅读:
    241. Different Ways to Add Parentheses
    332. Reconstruct Itinerary
    [LeetCode] 19. Remove Nth Node From End of List Java
    [LeetCode] 16. 3Sum Closest Java
    [LeetCode] 15. 3Sum Java
    [LeetCode] 11. Container With Most Water Java
    [LeetCode] 4. Median of Two Sorted Arrays
    [LeetCode] 3.Longest Substring Without Repeating Characters
    [LeetCode] 50. Pow(x, n) Java
    [LeetCode] 45. Jump Game II Java
  • 原文地址:https://www.cnblogs.com/lxwy1992/p/4745670.html
Copyright © 2011-2022 走看看