Michal Chudy
Michal Chudy

Reputation: 1893

How to distinguish if given Context object is an Activity or a Service Context?

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

Upvotes: 17

Views: 13146

Answers (1)

danh32
danh32

Reputation: 6244

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
}

Upvotes: 47

Related Questions