NotificationListenerService是通过系统调起的服务,在应用发起通知时,系统会将通知的应用,动作和信息回调给NotificationListenerService。但使用之前需要引导用户进行授权。使用NotificationListenerService一般需要下面三个步骤。
注册服务
首先需要在AndroIDManifest.xml对service进行注册。
<service androID:name=".NotificationCollectorService" androID:label="@string/app_name" androID:permission="androID.permission.BIND_NOTIFICATION_ListENER_SERVICE"> <intent-filter> <action androID:name="androID.service.notification.NotificationListenerService" /> </intent-filter></service>
继承实现NotificationListenerService
自己实现一个继承NotificationListenerService的service,在onNotificationPosted中完成自己需要的 *** 作。
public class NotificationCollectorService extends NotificationListenerService { @OverrIDe public voID onNotificationPosted(StatusbarNotification sbn) { Log.i("xiaolong","open" + "-----" + sbn.getPackagename()); Log.i("xiaolong","open" + "------" + sbn.getNotification().tickerText); Log.i("xiaolong","open" + "-----" + sbn.getNotification().extras.get("androID.Title")); Log.i("xiaolong","open" + "-----" + sbn.getNotification().extras.get("androID.text")); } @OverrIDe public voID onNotificationRemoved(StatusbarNotification sbn) { Log.i("xiaolong","remove" + "-----" + sbn.getPackagename()); }}
引导用户进行授权
由于此服务需要用户手动进行授权,所以使用前需要对用户进行引导设置。
public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); String string = Settings.Secure.getString(getContentResolver(),"enabled_notification_Listeners"); if (!string.contains(NotificationCollectorService.class.getname())) { startActivity(new Intent( "androID.settings.ACTION_NOTIFICATION_ListENER_SETTINGS")); } }}
用户授权后就可以对通知栏的所有信息进行监听了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android使用NotificationListenerService监听通知栏消息全部内容,希望文章能够帮你解决android使用NotificationListenerService监听通知栏消息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)