13

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 which logs Toast messages. Logging which application showed it and what the message displayed contained.

This is an entirely personal endeavour to create an app which can detect the toast messages. Because something on my phone is creating a toast saying "Sending..." about once per day, and for the life of me I can't track down the offending application (Service class). I thought it might be GMail or Evernote, but there toast messages for sending are slightly different. I'm going for building an app because 1) I don't know if LogCat would show anything, and 2) I don't want to keep my personal/dev phone plugged in to a PC all the time (as the "Sending..." message occurs so infrequently).

JonWillis
  • 3,146
  • 5
  • 32
  • 54
  • Did you finish your app and put it on Google Play? – activout.se Nov 23 '13 at 20:51
  • @divideandconquer.se I never posted it on google play as I was just tracking down the issue. But it did work. The message was coming from Sim Tool Kit which was pre-installed. I never bothered with google play as since android 4.1 the notifications tell you which app created them. – JonWillis Dec 01 '13 at 18:37
  • 5
    @divideandconquer.se I've made an app out of this question: https://play.google.com/store/apps/details?id=org.mars3142.android.toaster – mars3142 Jan 21 '14 at 19:34
  • @mars3142 Any plans on updating your excellent app? It complains in Android 10. – neu242 Jul 30 '20 at 10:08
  • @neu242 I always wanted to rewrite it from scratch, but with Android 11 (which is in beta now) no app is allowed to send toasts from the background. So I don't know if it will be useful to do something on the app. - Can you explain what the Android 10 issues are? I never used it by myself for 4 years. – mars3142 Jul 30 '20 at 16:18
  • @mars3142 Interesting info about Android 11! So all apps will have to use the notification API instead? The app works great, but upon first startup it warns about that it should be updated for the latest android, check for update, bla bla. – neu242 Jul 31 '20 at 12:55
  • 1
    @neu242 Ah, I think I was wrong -> https://developer.android.com/preview/features/toasts - Custom toasts from background will be blocked. In August I get a new Pixel with Android 11, so I should update/rewrite the app and publish a v1.0. – mars3142 Jul 31 '20 at 16:21
  • @mars3142 Cool, thanks! – neu242 Aug 03 '20 at 07:43

1 Answers1

18

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+.

Floern
  • 33,559
  • 24
  • 104
  • 119
  • I will give it a go, and let you know the result – JonWillis May 19 '12 at 19:15
  • Took a while to get the service working. But it worked. The toast message sending... is coming from com.android.stk – JonWillis May 25 '12 at 07:36
  • works beautifully. Been looking for this for some time. once 4.0 hit the tables, things when a little funny, but this fixes it :) – Dakun Skye Aug 23 '12 at 07:12
  • The second if-statement solved my problem in 3.0+ where Accessibility sends toasts through the same event type as status bar notifications and I didn't know how to separate them. Thanks! – Pilot_51 Apr 14 '13 at 06:39