简体   繁体   English

如何添加Class类型的参数<? extends broadcastreceiver>

[英]how to add parameter of type Class <? extends broadcastreceiver>

I have to use a method with this signature 我必须使用带有此签名的方法

BCR(String id, Class<? extends android.content.BroadcastReceiver> 
receiverClass))

I created a class extends a BroadcastReceiver as follows 我创建了一个扩展了BroadcastReceiver的类,如下所示

public class BCRGeoReference extends BroadcastReceiver {

public BCRGeoReference(Context ctx) {

}

@Override
public void onReceive(Context context, Intent intent) {

}
}

but I when i add BCRGeoReference to the method i receive an error saying: 但是当我将BCRGeoReference添加到方法时,我收到一条错误消息:

required java.lang.class<? extends android.content.BroadcastReceiver>

please let me know how to add that parameter to the method BCR() posted above 请让我知道如何将该参数添加到上面发布的方法BCR()中

Your signature is wrong. 您的签名是错误的。 You expect a class object with the generic type of ? extends BroadCastReceiver 您期望类对象具有通用类型? extends BroadCastReceiver ? extends BroadCastReceiver , but that is not the case. ? extends BroadCastReceiver ,但事实并非如此。 You want to pass an instance of BroadCastReceiver . 您要传递BroadCastReceiver的实例。

Change your signature to this 将您的签名更改为此

<T extends BroadcastReceiver> void BCR(String id, T receiverClass)

If you meant to use it as a class object then you have to pass a class. 如果您打算将其用作类对象,则必须传递一个类。 Something like 就像是

public class TestJava {
    void BCR(String id, Class<? extends BroadcastReceiver> receiverClass){

    }

    public class AnyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }

    public static void main(String args[]){
        TestJava test = new TestJava();
        test.BCR("s", AnyReceiver.class);
    }
}

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

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