Detecting toast messages

后端 未结 1 753
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 08:38

I don\'t think this is possible, as I haven\'t found anything in the SDK documentation (yet).

But I could do with knowing if its possible to write an application whi

相关标签:
1条回答
  • 2020-12-05 09:17

    It's possible to catch Messages/Notifications with an Accessibility Service, have a look at that.

    You can extend the class AccessibilityService and override the method onAccessibilityEvent() to implement something like this:

    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
            return; // event is not a notification
    
        String sourcePackageName = (String) event.getPackageName();
    
        Parcelable parcelable = event.getParcelableData();
        if (parcelable instanceof Notification) {
            // Statusbar Notification
        }
        else {
            // something else, e.g. a Toast message
            String log = "Message: " + event.getText().get(0) 
                       + " [Source: " + sourcePackageName + "]";
            // write `log` to file...
        }
    }
    

    Note: This didn't work for me on Android 2.2 as it doesn't seem to catch Toasts, but it worked on Android 4.0+.

    0 讨论(0)
提交回复
热议问题