![](/img/trans.png)
[英]How to pass a non static method from a specific class as parameter to Function
[英]how to pass class as a parameter to a static method
我正在创建一个控件类,以了解服务是否正在运行,如以下代码所示
public class CServiceCtrl {
private final static String TAG = ActMain.class.getSimpleName();
public static boolean isRunning(Activity act, Class servClass) {
ActivityManager manager = (ActivityManager) act.getSystemService(act.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (servClass.class.getName().equals(service.service.getClassName())) {//highlighted with red "unknown class servClass"
return true;
}
}
return false;
}
}
在主要活动的onCreate中,我调用以下代码,其中ServSimulator是服务的名称:
CServiceCtrl.isRunning(this, ServSimulator.class);
但是无法编译该代码,因为上述if语句中的servClass用红色突出显示,并且错误显示“未知类servClass”。 如何解决这个错误
servClass
已经是一个Class
对象。 不要在if
语句中使用servClass.class
; 只是servClass
本身。
if (servClass.getName().equals(service.service.getClassName()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.