Reputation: 400
I have 2 app, A and B, and i want send 2+2 from application A to B and in return i want to receive 4 from App B, please tell me the process and full code base.
Upvotes: 1
Views: 1318
Reputation: 473
1.From app A Trigger a broadCast1 with both of your numbers.
Intent intent = new Intent("com.myapps.appA");
intent.putExtra("num1",2);
intent.putExtra("num2",2);
sendBroadcast(intent);
now register the receiver for broadCast1 in App B, you can do this in onCreate of its Main activity.
private BroadcastReceiver broadcastReceiver1;
...
broadcastReceiver1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
registerReceiver(broadcastReceiver1, new
IntentFilter("com.myapps.appA");
Inside onRecieve get both the numbers from the intent and Trigger another broadCast with the result i.e.
int num1 = intent.getIntExtra("num1",0);
int num2 = intent.getIntExtra("num2",0);
Intent intent2 = new Intent("com.myapps.appB");
intent2.putExtra("sum",num1+num2);
YourActivity.this.sendBroadcast(intent2);
Now Register the reciever for Broadcast2 Inside your App A, you can do this in onCreate of its Main activity.
private BroadcastReceiver broadcastReceiver2;
...
broadcastReceiver2 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
registerReceiver(broadcastReceiver2, new
IntentFilter("com.myapps.appB");
Inside its OnRecive() get the result
int sum = intent.getIntExtra("sum",0);
Most importantly don't forget to unregister the receivers in onStop on the activity
Upvotes: 2