使用非静态内部类接收MainActivity中的广播

时间:2018-12-07 20:08:38

标签: java android

当用户与通知中的按钮交互时,我需要在活动中调用某些非静态方法,我试图在扩展BroadcastReceiver的独立类中进行操作,但是我不知道如何调用前面提到的活动的一种方法,我试图制作一个非静态内部类,它首先编辑了清单文件:

<receiver android:name=".Activity$NotificationBroadcast" >
    <intent-filter>
      .
      .
      .
    </intent-filter>
</receiver>

这给了我一个错误:

FATAL EXCEPTION: main
Process: com.example.app, PID: 3189
java.lang.RuntimeException: Unable to instantiate receiver 
com.example.app.Activity$NotificationBroadcast: 
java.lang.InstantiationException:

班级是:

public class NotificationBroadcast extends BroadcastReceiver {
    @Override
    public  void onReceive(Context context, Intent intent){
        if (intent.getAction().equals(Activity.ACTION1)){
            // do things
        } else if (intent.getAction().equals(Activity.ACTION1)){
            // do things
        }else if (intent.getAction().equals(Activity.ACTION2)){
            // do things
        }else if (intent.getAction().equals(Activity.ACTION3)){
            // do things
        }else if (intent.getAction().equals(Activity.ACTION4)){
            // do things
        }
    }
}

1 个答案:

答案 0 :(得分:0)

恐怕您不能将接收器作为内部类,因为当接收器被静态实例化时,“ holder”类也必须被实例化。仅当活动实例处于活动状态时才创建活动实例,这就是为什么您会得到例外的原因。

如果希望接收者与活动进行交互(通过在活动类中调用非静态方法),建议您将接收者设为非静态对象。这意味着您需要在OnCreate()中注册接收者的实例,并在OnDestroy()中注销它的实例。

为了更好的设计,应该将活动实例通过其构造函数作为接口传递给接收器,以便接收器无法访问整个活动对象,而只能访问功能。

清单应具有:

<receiver android:name=".NotificationBroadcast" android:enabled="false" />

交互界面(例如IWorker.java):

public interface IWorker {
    public void doThis();
    public void doThat();
}

接收器(一个自己的类)接收IWorker并在接收广播时执行某些操作:

public class NotificationReceiver extends BroadcastReceiver {
    public static final string ACTION1 = "com.yourpackage.action1";
    public static final string ACTION2 = "com.yourpackage.action2";

    private IWorker worker;

    public NotificationReceiver() {
    }

    public NotificationReceiver(IWorker worker) {
        this.worker = worker;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION1)) {
            worker.doThis();
        } else if (intent.getAction().equals(ACTION2)) {
            worker.doThat();
        }
    }
}

该活动在接收者的生命周期内负责:

public class MyActivity extends Activity implements IWorker {
    private NotificationReceiver receiver;

    @override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // create the receiver
        receiver = new NotificationReceiver(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction(NotificationReceiver.ACTION1);
        filter.addAction(NotificationReceiver.ACTION2);

        // register it
        registerReceiver(receiver, filter);
    }        

    @override
    protected void onDestroy() {
        super.onDestroy();

        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
    }

    @override
    public void doThis() {
        System.out.println("Doing this...");
    }

    @override
    public void doThat() {
        System.out.println("Doing that...");
    }
}

P.S。以上代码仅供参考,未经测试,可能无法编译。