简体   繁体   English

如何将类作为参数传递给静态方法

[英]how to pass class as a parameter to a static method

i am creating a control class to know if a service is running or not as shown in the following code 我正在创建一个控件类,以了解服务是否正在运行,如以下代码所示

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

and in onCreate of the main activity I call the follwoing code where ServSimulator is the name of the service: 在主要活动的onCreate中,我调用以下代码,其中ServSimulator是服务的名称:

CServiceCtrl.isRunning(this, ServSimulator.class);

but the code cant be compiled because the servClass in the if-statement above is underscored in red and the error says "unknown class servClass". 但是无法编译该代码,因为上述if语句中的servClass用红色突出显示,并且错误显示“未知类servClass”。 how to solve this error 如何解决这个错误

servClass is already a Class object. servClass已经是一个Class对象。 Don't use servClass.class in the if statement; 不要在if语句中使用servClass.class just servClass by itself. 只是servClass本身。

if (servClass.getName().equals(service.service.getClassName()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM