0

只是出于好奇。如何从构造函数不需要参数的 Class 创建一个新类?这是一个例子:

public static BroadcastReceiver startBroadcastReceiver(Context context, Class<?> receiverClass, String receiverTag) {
    BroadcastReceiver receiver = null;

    IntentFilter intentFilter = new IntentFilter(receiverTag);

    receiver = new receiverClass(); // <---- What's the correct syntax?

    if (receiver != null && intentFilter != null) {
        context.registerReceiver(receiver, intentFilter);
    }

    return receiver;
}

我是这样称呼它的:

MyTools.startBroadcastReceiver(context, MyBroadcastReceiver, MyBroadcastReceiver.TAG);

提前致谢。

4

1 回答 1

0

用这个:

try{
    receiver = (BroadcastReceiver) receiverClass.newInstance();
}catch(Exception e){
    //do stuff
}

你会叫它

MyTools.startBroadcastReceiver(context, MyBroadcastReceiver.class, MyBroadcastReceiver.TAG);
于 2012-08-30T10:48:59.980 回答