0

I am creating some classes that do not implement threading logic but need to be used off the main thread, so I want to throw NetworkOnMainThreadException (or a similar one for DB operations) in their constructors. How do I do that? How do Android do that, can I find it somewhere in the source code?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

1 Answers1

2

you could check the current's thread looper. E.g.

if (Looper.myLooper() == Looper.getMainLooper()) {
   throw new NetworkOnMainThreadException();
}

I would avoid it anyway. If I were in you I would document the method/class to clarify it has to be executed on an asynchronously

Blackbelt
  • 156,034
  • 29
  • 297
  • 305