介绍
在Android 4.3(API 18)中引入了NotificationListenerService 。 它允许应用程序在创建或删除时接收有关通知的信息。 NotificationListenerService类是从Service类派生的。 它有两个抽象方法,即1. onNotificationPosted 2. onNotificationRemoved。
要使用NotificationListenerService,我们需要创建一个扩展NotificationListenerService并实现两个回调方法的Java文件。 这两个方法都有一个名为“ sbn”的参数,它是StatusBarNotification类的对象。 StatusBarNotification提供有关通知的必要信息。
NotificationListenerService提供了使用getActiveNotifications获取活动通知的功能,并且还提供了使用cancelAllNotifications删除通知的功能。
有用的方法
-
NotificationListenerService
onNotificationPosted()
cancelAllNotifications() -
StatusBarNotification
getId()
getPackageName()
注意:用户需要从“设置>安全>通知访问”中启用通知权限。
NotificationListenerService的源代码
值得一看的源代码,如果您好奇的话。 它有两个Java文件和两个AIDL文件: https ://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/service/notification
例
这是NotificationListenerService的简单示例,它具有包含三个按钮和一个textview的简单UI。
- 创建通知-将创建简单的通知,以便我们可以测试onNotificationPosted事件
- 清除所有通知–它将在通知栏中创建所有通知
- 通知列表–将在文本视图中显示通知列表
- TextView –显示通知事件和通知列表。
此示例具有活动,服务和广播接收器。 BroadcastReceiver,用于活动和服务之间的通信。 我们无法直接从活动中访问cancelAllNotifications()和getActiveNotifications()方法,因此我使用BroadcastReceivers。
屏幕截图
源代码
1. MainActivity.java
package com.kpbird.nlsexample;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtView;
private NotificationReceiver nReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.textView);
nReceiver = new NotificationReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
registerReceiver(nReceiver,filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(nReceiver);
}
public void buttonClicked(View v){
if(v.getId() == R.id.btnCreateNotify){
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this);
ncomp.setContentTitle("My Notification");
ncomp.setContentText("Notification Listener Service Example");
ncomp.setTicker("Notification Listener Service Example");
ncomp.setSmallIcon(R.drawable.ic_launcher);
ncomp.setAutoCancel(true);
nManager.notify((int)System.currentTimeMillis(),ncomp.build());
}
else if(v.getId() == R.id.btnClearNotify){
Intent i = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
i.putExtra("command","clearall");
sendBroadcast(i);
}
else if(v.getId() == R.id.btnListNotify){
Intent i = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
i.putExtra("command","list");
sendBroadcast(i);
}
}
class NotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String temp = intent.getStringExtra("notification_event") + "n" + txtView.getText();
txtView.setText(temp);
}
}
}
2. NLService.java
package com.kpbird.nlsexample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
private NLServiceReceiver nlservicereciver;
@Override
public void onCreate() {
super.onCreate();
nlservicereciver = new NLServiceReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kpbird.nlsexample.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
registerReceiver(nlservicereciver,filter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(nlservicereciver);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"********** onNotificationPosted");
Log.i(TAG,"ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText + "t" + sbn.getPackageName());
Intent i = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "n");
sendBroadcast(i);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG,"********** onNOtificationRemoved");
Log.i(TAG,"ID :" + sbn.getId() + "t" + sbn.getNotification().tickerText +"t" + sbn.getPackageName());
Intent i = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
i.putExtra("notification_event","onNotificationRemoved :" + sbn.getPackageName() + "n");
sendBroadcast(i);
}
class NLServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra("command").equals("clearall")){
NLService.this.cancelAllNotifications();
}
else if(intent.getStringExtra("command").equals("list")){
Intent i1 = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
i1.putExtra("notification_event","=====================");
sendBroadcast(i1);
int i=1;
for (StatusBarNotification sbn : NLService.this.getActiveNotifications()) {
Intent i2 = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
i2.putExtra("notification_event",i +" " + sbn.getPackageName() + "n");
sendBroadcast(i2);
i++;
}
Intent i3 = new Intent("com.kpbird.nlsexample.NOTIFICATION_LISTENER_EXAMPLE");
i3.putExtra("notification_event","===== Notification List ====");
sendBroadcast(i3);
}
}
}
}
3. activity_main.xml
4. AndroidManifest.xml
用户按照以下代码打开“通知访问”设置屏幕
Intent intent=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
- 从GitHub下载源代码
翻译自: https://www.javacodegeeks.com/2013/10/android-notificationlistenerservice-example.html