问题描述
我正在从应用程序A向应用程序B发送广泛广播,应用程序B具有BroadCastReceiver
来处理意图.
I am sending a broadCast from App A to app B, app B has a BroadCastReceiver
to handle intent.
现在,我在App B的BroadcastReceiver
的onReceive
中进行了一些操作,然后我想将结果返回给App A,这样我就可以进行回调,并在结果为正数时继续操作.
Now I do some operation in onReceive
in App B's BroadcastReceiver
and then I want to get result back to App A, so that I can have a callback, and continue operations if result is positive.
在一些代码下面.
在应用B内部:
public class TripBackupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
System.out.println("broadcast Received");
// send result back to Caller Activity
}
}
内部应用A:
Intent intent = new Intent("a_specified_action_string");
sendBroadcast(intent);
有人可以解决这个问题吗,请向我提出一些建议.谢谢!
Can anybody have any solution for this problem, please guide me with some suggestions. Thanks!!
推荐答案
我正在寻找类似的东西. 这极大地帮助了我.
I was looking for something similar. This helped me tremendously.
基本上,代替使用sendBroadcast(Intent),请尝试使用合适的sendOrderedBroadcast()形式,该形式允许设置BroadcastReceiver来处理Receiver的结果.一个简单的例子:
Basically, instead of a sendBroadcast(Intent) try using a suitable form of sendOrderedBroadcast() which allows setting a BroadcastReceiver to handle results form a Receiver. A simple example:
sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
Bundle results = getResultExtras(true);
String trail = results.getString("breadcrumb");
results.putString("breadcrumb", trail + "->" + "sometext");
Log.i(TAG, "Final Result Receiver = " + results.getString("breadcrumb", "nil"));
}
}, null, Activity.RESULT_OK, null, null);
这篇关于结果从广播接收器返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!