从一个BroadcastReceiver一流的呼叫活动的方法方法、BroadcastReceiver

2023-09-12 06:31:29 作者:爱在西元

我知道我可以做一个内部接收器类来从我的接收器调用的任何方法

I know I can do an inner receiver class to call any method from my receiver

但我的主要活动是太他妈的大,做了很多的事情。 所以,我需要一个扩展广播接收器,但谁不是一个内部类的类。并且可以从我的主要活动调用的方法。我不知道这是否是可能的,但我的活动是一个家庭活动和singleInstance活动,所以也许这个细节某人有一种方法来访问我的活动。

But my main activity is too damn big and does a lot of things. So I will need a class that extends broadcast receiver but who isn't an inner class. And can call one method from my main activity. I don't know if it's possible but my activity is a Home activity and a "singleInstance" activity so maybe with this detail someone has a way to access to my activity.

如果这是不可能的任何方式来分割一些Java code在多个文件中我的主要有超过600线。 (和信息,我已经19 java文件为我的应用程序的测试版本,所以我也尽量分开吧)

If it's impossible any way to split some java code in multiple files my main have more than 600 lines. (and for information I have already 19 java files for an alpha version of my apps so I have try to split it)

推荐答案

动态创建的BroadcastReceiver:

Create the BroadcastReceiver dynamically:

在你的BroadcastReceiver类中定义的类成员:

In your BroadcastReceiver class define class member:

YourMainActivity yourMain = null;  

和方法:

setMainActivityHandler(YourMainActivity main){
yourMain = main;
}  

从MainActivity做的:

from your MainActivity do:

private YourBroadcastReceiverClassName yourBR = null;
yourBR = new YourBroadcastReceiverClassName();
    yourBR.setMainActivityHandler(this);    
    IntentFilter callInterceptorIntentFilter = new           IntentFilter("android.intent.action.ANY_ACTION");
    registerReceiver(yourBR,  callInterceptorIntentFilter);

最后,当yourBR.onReceive被激发您可以拨打:

finally, when yourBR.onReceive is fired you can call:

yourMain.methodOfMainActivity();