Clear another app's notification via the Acces

2019-08-18 18:11发布

My app is using the Accessibility API to catch notifications generated by other apps and act on them. I'd like to add a feature where the original notification (generated by some other app) can be cancelled.

The usual method of using the notification manager won't work since you have to be the one who created the notification to be able to clear it. The accessibility API lets me read a different app's notification but does it allow you to clear it as well?

Thanks!

2条回答
迷人小祖宗
2楼-- · 2019-08-18 18:44

No, you cannot clear other app's notifications (thankfully).

查看更多
We Are One
3楼-- · 2019-08-18 18:45
public void onAccessibilityEvent(AccessibilityEvent event) {
        // TODO Auto-generated method stub
        if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            //Do something, eg getting packagename
            final String packagename = String.valueOf(event.getPackageName());
            final String text = String.valueOf(event.getText());

            if(TARGET_PACKAGE.equals(packagename)){
                Notification n = (Notification) event.getParcelableData();

                try{
                        n.deleteIntent.send(this,0,new Intent());
                }catch(Exception e){e.printStackTrace();}
        }
} 

If deleteIntent of the notification was defined already, can cancel it.

查看更多
登录 后发表回答