Result Back From Broadcast Receiver
I am sending a broadCast from App A to app B, app B has a BroadCastReceiver to handle intent. Now I do some operation in onReceive in App B's BroadcastReceiver and then I want to g
Solution 1:
Why not just (sort of) send an Intent
from TripBackupReceiver
that is received by App A?
Something like:
publicclassTripBackupReceiverextendsBroadcastReceiver
{
@OverridepublicvoidonReceive(Context context, Intent intent) {
System.out.println("broadcast Received");
// send result back to Caller ActivityIntentreplyIntent=newIntent("a_specified_action_string");
context.sendBroadcast(replyIntent);
}
}
You could receive this Intent
in a BroadcastReceiver
defines in App A. Or alternatively use the Context.startActivity(Intent)
method to start an activity in App A.
You may need to implement onNewIntent()
if this makes a call to an activity that is already running.
Please use signed intents for this, in order to prevent users from hacking into your Intent API.
Solution 2:
I was looking for something similar. This helped me tremendously.
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, newBroadcastReceiver() {
@SuppressLint("NewApi")@OverridepublicvoidonReceive(Context context, Intent intent) {
Bundleresults= getResultExtras(true);
Stringtrail= results.getString("breadcrumb");
results.putString("breadcrumb", trail + "->" + "sometext");
Log.i(TAG, "Final Result Receiver = " + results.getString("breadcrumb", "nil"));
}
}, null, Activity.RESULT_OK, null, null);
Post a Comment for "Result Back From Broadcast Receiver"