public class NotificationListenerExampleService extends NotificationListenerService {
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn){
//I am sucessfully Receiving Notification
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn){
}
}
我可以使用 onNotificationPosted() 读取通知内容。如何按通知中的“标记为已读”按钮?
最佳答案
after spending some time i am able to do that with the help of below code.
public class NotificationListenerExampleService extends
NotificationListenerService {
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn){
//I am sucessfully Receiving Notification
try {
if (sbn.getNotification().actions != null) {
for (Notification.Action action: sbn.getNotification().actions) {
if (action.title.toString().equalsIgnoreCase("Mark as read")) {
PendingIntent intent = action.actionIntent;
try {
intent.send();
} catch(PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn){
}
}
关于java - 如何借助NotificationListnerService在通知中按下“标记为已读”按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59733204/