开发者

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

开发者 https://www.devze.com 2023-02-03 00:47 出处:网络
I would like to know if my give开发者_JAVA技巧n 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

I would like to know if my give开发者_JAVA技巧n 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.)


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消