简体   繁体   中英

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

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:

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

please let me know how to add that parameter to the method BCR() posted above

Your signature is wrong. You expect a class object with the generic type of ? extends BroadCastReceiver ? extends BroadCastReceiver , but that is not the case. You want to pass an instance of 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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