17

I would like to know if my given Context object is from Activity, Service or Application. Or in other words if my code is executing in background or in foreground. (By foreground i mean Activity code and threads that have been created by Activity.)

2
  • 2
    This is probably not at all what you want. What are you you trying to do, and why would you possibly not know where your code is running. And even if you didn't, why does it matter? It sounds like you have some serious design flaws. You should also note that it's perfectly normal for Activity code to run in the "background"
    – Falmarri
    Jan 4, 2011 at 18:48
  • It's because I want sometimes block data transfer when it's in background, invoked by Service. But you might be right that I should think about other solution. Jan 4, 2011 at 20:52

1 Answer 1

47

You should be able to test if an object is a specific class using "instanceof"

if (context instanceof Activity) {
  // handle activity case
} else if (context instanceof Service){
  // handle service case
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.