大佬教程收集整理的这篇文章主要介绍了Android 通知使用权(NotificationListenerService)的使用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Android 通知使用权(NotificationListenerservicE)的使用
简介
当下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它们能管理过滤系统中的一些无用消息,使得消息栏更清爽干净。其实此功能的实现便是使用了Android中提供的通知使用权权限。Android4.3后加入了通知使用权NotificationListenerservice,就是说当你开发的APP拥有此权限后便可以监听当前系统的通知的变化,在Android4.4后还扩展了可以获取通知详情信息。下面我们来看看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)的使用全部内容,希望文章能够帮你解决Android 通知使用权(NotificationListenerService)的使用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。