如何在不使用onCreate()的情况下将数据从BroadcastReceiver传递到Activity [英] How to pass data from BroadcastReceiver to Activity without in onCreate()
问题描述
我对将数据从BroadcastReceiver
传递到Activity
有一个严重的问题.让我们仔细看看我的问题.我有一个PhoneStateReceiver extends BroadcastReceiver
类别,该类别曾经用来接收传入的电话.
I have a serious issue about passing data from BroadcastReceiver
to an Activity
. Let see my issue carefully. I have a class PhoneStateReceiver extends BroadcastReceiver
that used to received an incoming phone.
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
传入电话将被发送到名为ReceiverActivity
的Activity
. ReceiverActivity
接收传入的电话,并通过套接字连接将其发送到服务器.套接字连接在onCreate
函数中初始化.我在Google上搜索并找到了将数据从BroadcastReceiver
传递到Activity
的服务器方法.常见的方法是通过putExtra
函数发送数据并调用startActivity
.但是,该方式将再次调用onCreate
,然后连接套接字,再次绘制UI.因此,这对我的情况没有帮助.
The incoming phone will be sent to an Activity
, called ReceiverActivity
. The ReceiverActivity
receives the incoming phone and sends it to a server via a socket connection. The socket connection is initialized in the onCreate
function. I googled and found server way to pass the data from BroadcastReceiver
to an Activity
. The common way is that send data via putExtra
function and call startActivity
. However, the way will call the onCreate
again and then connect the socket, draw the UI again. Thus, it is not helpful in my case.
在我的目标中,如果电话收到来电,它将把来电发送到ReceiverActivity
. ReceiverActivity
接收消息并调用发送功能.哪个是最好的方法?谢谢
In my goal, If the phone receives an incoming call, it will send the incoming call to the ReceiverActivity
. The ReceiverActivity
receives the message and calls the send function. Which is the best way to do it? Thank you
将数据从BroadcastReceiver
传递到ReceiverActivity
的常用方法,如下所示:
The common way to pass data from a BroadcastReceiver
to a ReceiverActivity
that I used as follows
在 PhoneStateReceiver 类中:
Intent intent_phonenum = new Intent(context, ReceiverActivity.class);
intent_phonenum.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent_phonenum.putExtra("phone_num", incomingNumber);
context.startActivity(intent_phonenum);
在 ReceiverActivity 类中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connect_socket();
Intent intent = getIntent();
phone_num = intent.getStringExtra("phone_num");
send(phone_num);
}
推荐答案
有一个非常简单的设计模式,您可以在这里使用它来简化类之间的通信,并解耦代码:publisher/subscriber
.我最喜欢的库是EventBus
:
There is a very simple design pattern you can use here to ease communication between your classes and also decouple your code: publisher/subscriber
. My favorite library for this is EventBus
:
首先,将其添加到您的 build.gradle 文件中:
First, add to your build.gradle file:
compile 'org.greenrobot:eventbus:3.0.0'
然后,像这样创建一个简单的POJO - Plain Old Java Object
:
Then, create a simple POJO - Plain Old Java Object
like this:
public class OnReceiverEvent{
private String phoneNumber;
public OnReceiverEvent(String phone){
this.phoneNumber = phone;
}
public String getPhoneNumber(){
return phoneNumber;
}
}
接下来,通过将Receiver
类设置为publisher
,将Activity
类设置为subscriber
,您应该能够轻松地将信息传递给活动,如下所示:
Next, by making your Receiver
class a publisher
, and your Activity
a subscriber
, you should be able to easily pass the information to your activity like this:
//inside your PhoneStateReceiver class when you want to pass info
EventBus.getDefault().post(new OnReceiverEvent(phoneNumber));
接下来,在您的活动中,只需执行以下操作:
Next, inside your activity, simply do this:
//onStart
@Override
public void onStart(){
super.onStart();
EventBus.getDefault().register(this);
}
//onStop
@Override
public void onStop(){
super.onStop();
EventBus.getDefault().unregister(this);
}
最后,处理发布的数据,即phoneNumber值:
Finally, handle the posted data i.e phoneNumber value:
@Subscribe
public void onPhoneNumberReceived(OnReceiverEvent event){
//get the phone number value here and do something with it
String phoneNumber = event.getPhoneNumber();
//display or something?
}
更新
如果您要订阅此活动的另一个事件,只需使用@Subscribe
注释创建与第一个事件类似的方法即可.
If you have another Event that you want this activity to subscribe to, simply create a method like you did in the first one using the @Subscribe
annotation.
@Subscribe
public void onSomeOtherEvent(EventClassName event){
//get the variables here as usual;
}
这是将数据从接收器传递到活动的最简单方法,而不必担心一遍又一遍地开始活动!
This is the easiest way to pass the data from your receiver to your activity without having to worry about starting the activity over and over!
我希望这会有所帮助,祝你好运!
I hope this helps and good luck!
这篇关于如何在不使用onCreate()的情况下将数据从BroadcastReceiver传递到Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!