温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中NotificationListenerService怎么用

发布时间:2021-08-09 11:04:22 来源:亿速云 阅读:131 作者:小新 栏目:移动开发

这篇文章主要介绍了Android中NotificationListenerService怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

使用

新建一服务类,使它继承NotificationListenerService,并实现两个重要的方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public class NotificationListener extends NotificationListenerService { 
  privatestatic final String TAG = "NotificationListener"; 
  
  @Override 
  public void onNotificationRemoved(StatusBarNotification sbn) { 
    Log.i(TAG,"Notification removed"); 
  } 
  
  @Override 
  public void onNotificationPosted(StatusBarNotification sbn) { 
    Log.i(TAG, "Notification posted"); 
  } 
}

AndroidManifest.xml中声明此服务类,并必须声明BIND_NOTIFICATION_LISTENER_SERVICE许可和意图过滤器

android.service.notification.NotificationListenerService,还有我们在系统设置中通知使用权列表中看到的label标签:

<serviceandroid:name=".NotificationListener" 
     android:label="通知使用权测试程序" 
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 
  <intent-filter> 
    <actionandroid:name="android.service.notification.NotificationListenerService"/> 
  </intent-filter> 
 </service>

OK,就这么简单就可以完成APP监听系统通知栏的功能了。接下来,我们来看看NotificationListenerService类还提供了一些重要的方法:

StatusBarNotification[] sbns = getActiveNotifications();         // 返回当前系统所有通知的数组 
cancelAllNotifications();                        // 删除系统中所有可被清除的通知 
cancelNotification(String pkg, String tag, int id);           // 删除具体某一个通知

还有上面我们提到的两个重要的重写方法:

onNotificationRemoved(StatusBarNotification sbn);            // 通知被移除时回调 
onNotificationPosted(StatusBarNotification sbn);             // 增加一条通知时回调

这两个重要的回调方法它们的参数StatusBarNotification对象是当前触发变化通知的详细信息。来看下StatusBarNotification的使用:

sbn.getId();                               // 返回通知对应的id 
sbn.getNotification();                          // 返回通知对象 
sbn.getPackageName();                          // 返回通知对应的包名 
sbn.getPostTime();                            // 返回通知发起的时间 
sbn.getTag();                              // 返回通知的Tag,如果没有设置返回null 
sbn.isClearable();                            // 返回该通知是否可被清楚,是否为FLAG_ONGOING_EVENT、FLAG_NO_CLEAR 
sbn.isOngoing();                             // 返回该通知是否在正在运行的,是否为FLAG_ONGOING_EVENT

其中,getNotification()返回的通知对象,还可以详细看到通知的其它相关信息,如:

Notification notification = sbn.getNotification(); 
notification.contentView;                        // 通知的RemoteViews 
notification.contentIntent;                       // 通知的PendingIntent 
notification.actions;                          // 通知的行为数组 
// Android4.4后还扩展了可以获取通知详情信息 
if (Build.VERSION.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     Bundle extras = notification.extras; 
     String notificationTitle = extras.getString(Notification.EXTRA_TITLE); 
     int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON); 
     Bitmap notificationLargeIcon = ((Bitmap)extras.getParcelable(Notification.EXTRA_LARGE_ICON)); 
     CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT); 
     CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT); 
}

跳转系统设置里的通知使用权页面

private boolean gotoNotificationAccessSetting(Contextcontext) { 
  try { 
    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
    return true; 
  } catch(ActivityNotFoundException e) { 
    try { 
      Intent intent = new Intent(); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings$NotificationAccessSettingsActivity"); 
      intent.setComponent(cn); 
      intent.putExtra(":settings:show_fragment", "NotificationAccessSettings"); 
      context.startActivity(intent); 
      return true; 
    } catch(Exception ex) { 
      ex.printStackTrace(); 
    } 
    return false; 
  } 
}

判断是否拥有通知使用权

private boolean notificationListenerEnable() { 
  boolean enable = false; 
  String packageName = getPackageName(); 
  String flat= Settings.Secure.getString(getContentResolver(),"enabled_notification_listeners"); 
  if (flat != null) { 
    enable= flat.contains(packageName); 
  } 
  return enable; 
}

感谢你能够认真阅读完这篇文章,希望小编分享的“Android中NotificationListenerService怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI