3

I'm developing an app that intercepts toast messages and displays them in a custom view.

The interception part is easily implemented using the AccessibilityService.

The tricky part is how to suppress the toast messages from appearing (or at least make them invisible). I know (to the best of my understanding) that this is not possible in normal circumstances.

But does anyone know any hacks that would allow me to suppress the toasts from appearing or make them invisible?

Would it be possible with root permission?

Note: I'm relatively new to programming. Nonetheless, I had a look at the android source code for Toast.java (link). Would it be possible to intercept the show() method or any other relevant method to stop them from appearing (maybe with root permission)?

fahmy
  • 3,543
  • 31
  • 47
  • When you use the `AccessibilityService` to intercept the toast, what does it give you? An instance of the toast itself? Could you then `cancel()` it? – neildeadman Aug 05 '13 at 09:57
  • `AccessibilityService` receives an event. See this [answer](http://stackoverflow.com/a/10665625/2153244). I tried casting it to a toast, but when I try to call Toast.Cancel() I get an error. – fahmy Aug 05 '13 at 12:08

1 Answers1

2

According to Dianne Hackborn here, you can't. Yes, I realize that post is over four years old, but I haven't seen anything in the SDK since to overcome that.

Yes, you can use an accessibility service to detect toasts, but that's only so you can do something in addition, like TTS, not to stop them.

It would be a bad day if you could. For example, junk apps that pastes shortcuts on your desktop. Root apps that require SuperUser. In both cases, a toast pops up to let you know that something out of the ordinary is happening. That's a good thing, and if I ever found an app that was suppressing toasts, it would be gone instantly.


Moral issues aside, you could do this if you changed the OS source and compiled it on your own, but it would still only work for users that had that particular ROM installed.

As another option, have you tried creating your own Toast instead? You can give it a custom View, and if it's timed right, it will show up just after the normal one. Then it would overlap and you won't see the first. I'm not sure how feasible it is, but it's a thought.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • The moral reasoning for toasts is understood. Changing the OS source to make it compatible with an app...? Don't think that app would be very popular. I'll try overlapping a custom toast over the system default, but skeptical about guaranteeing the same result for all android phones. Thanks! – fahmy Aug 06 '13 at 09:02
  • I agree, never said it would be popular. You didn't say whether this was just a side project, or other limited-scope app, so you never know. For the market at large, requiring a specific ROM would indeed be a death sentence. Also skeptical of the overlay, but it was the only hack I could think of. – Geobits Aug 06 '13 at 12:15