How to send data from 1 app to another in android - android

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.

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

Related

How to unregister multiple Broadcast receiver which are in different classes at a time?

i want to unregister some broadcast receivers with single click.here is the flow.. lets say in Activity A i have below broadcast receivers.
public BroadcastReceiver upload = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
public BroadcastReceiver download = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
and in Activity B i have below broadcast receivers
public BroadcastReceiver wifi = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
public BroadcastReceiver data = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
my problem is i want to unregister all these BroadcastReceiver from Activity C with a button click. How can i do that? and how can i check is receiver is registered or not..?
Keeping broadcast receiver register even after activity is out of screen is memory leak and you should not do that.
Always register your broadcast receivers in onStart/onCreate/onResume and unregister them in onStop/onDestroy/onPause.
Why do you need to keep receiver active even in case activity is out of screen? You might as well use Android Service if you want something to execute out of activity scopes.
Create an Interface in your Activity C with a method unresisterRegisters()
Implement this Interface In Activity A and B. Overrrid the method and write code for unregistere Receivers
create object of A and B in Activity C inside OnButton click and call unresisterRegisters() method with both methods.
I hope this will help You.

Put onReceive on wait until the started activity finishes

In the onReceive(), I want to create a new Intent and start a new Activity. After the activity finishes, I want to get back to the same line from where I left for the activity. I know that I cannot use BroadcastReceiver class for achieving this. How can this be achieved? Below is the onReceive() code:
final BroadcastReceiver mPairingRequestRecevier = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction()))
{
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
if (type == BluetoothDevice.PAIRING_VARIANT_PIN)
{
abortBroadcast();
String passkey="hey";
Intent passkeyIntent = new Intent(context.getApplicationContext(),TestActivity.class);
passkeyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
passkeyIntent.putExtra("passkey",passkey);
context.startActivity(passkeyIntent);
Log.d("after returning...",passkey);
}
else
{
Log.d("Unexpected pairingtype:" , type+"");
}
}
}
};
Any idea that can achieve this would be appreciated.
You can use startActivityFor result instead of startActivity to start an activity..make it perform some task and return the result to previous activity
Look at this: How to manage `startActivityForResult` on Android?

Multiple custom broadcasting (how to handle)

I want to add a custom broadcast receiver to my app. and I have 3 methods that should do the broadcast. and in my onReceive method i want to identify which method did the broadcast. I have 3 methods like this
public void method01(View v){
int flag = 1;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
public void method02(){
int flag = 2;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
public void method03(){
int flag = 3;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
and this is my broadcastIntent method
public void broadcastIntent(Intent intent){
sendBroadcast(intent);
}
in my onReceive method i use getFlags() method to get the flag value from the intent and send it through a if, else. but this do not work. any suggestion for improvements are welcome.
You can also use Actions to identify each one of you Intent objects.
String action1 = "first_sender";
String action2 = "second_sender";
Intent createIntent01(){
Intent intent01 = new Intent();
intent01.setAction(action1);
return intent01;
}
Intent createIntent02(){
Intent intent02 = new Intent();
intent01.setAction(action2);
return intent02;
}
And in your onReceive method you can use the getAction() method of intents to check which action was sent. This is in case you're not already using Actions.
[[EDIT]]
To register a BroadcastReceiver you need to define an IntentFilter and register the actions you wish to receive this way:
mBroadcastReceiver broadcastReceiver = new mBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action1);
intentFilter.addAction(action2);
registerReceiver(broadcastReceiver,intentFilter);
class mBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context arg0, Intent arg1){
String action = arg1.getAction();
if(action.equals(action1)){
//do something
}else if(action.equals(action2)){
//do something else
}
}
The first problem is that ypu didn't specify a target for your intent. You can use intent filters and actions like rodkarom suggested or specify receiver's class directly (see in my example). In both cases you need either to declare your broadcast receiver in AndroidManifest.xml, or register it at runtime (see rodkarom's answer for a sample).
The method addFlags is used to specify some internal properties of Intent (like start activity corresponding to this intent in a new task) , so you cannot use it for your own data. The list of possible flags is in the documentation for setFlags method.
You can use putExtra to achieve your goal:
// an Activity is just an example
public class SenderActivity extends Activity {
// ...
void method01() {
int flag = 1;
Intent intent = new Intent(getApplicationContext(), Receiver.class); // any Context is acceptable here
intent.putExtra(MyReceiver.EXTRA_FLAG, flag); // any string will do well, you just need it to be the same here and in getExtra later
sendBroadcast(intent);
}
}
public class MyReceiver extends BroadcastReceiver {
public static final String EXTRA_FLAG = "your.package.name.EXTRA_FLAG";
// and in onReceive
public void onReceive (Context context, Intent intent) {
int flag = intent.getIntExtra(EXTRA_FLAG, someDefaultValue);
if (flag == 1) {
// ...
}
// ...
}
}
I found out the way to this and thought of sharing the code. this is the broadcasting done in my main activity for 2 different methods.
public void method1(View view){
Intent intent = new Intent();
intent.setAction("method1");
sendBroadcast(intent);
}
public void method2(View view){
Intent intent = new Intent();
intent.setAction("method2");
sendBroadcast(intent);
}
and this is how i receive it..
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Intent Detected.",Toast.LENGTH_LONG).show();
}
this is how i registered it on manifest.
<receiver android:name="Receiver" >
<intent-filter>
<action android:name="method1" >
</action>
<action android:name="method2" >
</action>
</intent-filter>
</receiver>
Hope this will help if any one else came up with similar problem. and big thank you to every one who posted their answers here.

BroadcastReceiver receives more Intents than sent

I have next problem:
I create a custom BroadcastReceiver and register it in main activity onCreate handler:
public class MainActivity extends ListActivity {
private static final LogReceiver logReceiver = new LogReceiver();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(this).registerReceiver(logReceiver, new IntentFilter(LogReceiver.ACTION_LOG));
}
...
}
BroadcastReceiver class:
public class LogReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("intent", "intent <-");
}
}
Then I send multiple Intents from a local service using next code:
Intent intent = new Intent(LogReceiver.ACTION_LOG).putExtra("log", logString);
localBroadcastManager.sendBroadcast(intent);
Log.i("intent", "intent ->");
The log looks very strange for me:
intent ->
intent <-
intent ->
intent <-
intent <-
intent ->
intent <-
intent <-
intent <-
...
The 1st time LogReceiver receives only one Intent and this is correct but each next time it receives more equal Intents than sent.
I can switch from Intent approach in my project but just interesting why this happens?
You should deregister the receiver in the onDestroy of the Activity.
Additionally you should check if this receiver really has to be static.
If you need the receiver throughout the application it should be saved in a separate singleton class or in a custom Application class instead of one Activity.
You could then register the receiver in the first started Activity. But you should make sure it is registered only once for example by storing a boolean registered into the receiver that is checked before registering at the LocalBroadcastManager.

send action from receiver to activity?

I am using broadcast receiver in my app to detect incomming call and it works fine. But problem is I can not send action to activity. I mean.. I want do something in activity not in receiver. I read many tutorial but they all are performing action in receiver. Any idea ?
You can declare a BroadcastReceiver as inner class of the Activity. In this case you can directly call activity's methods:
public class MyActivity extends Activity {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
activityMethod();
}
};
private final IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
#Override
protected void onStart() {
super.onResume();
registerReceiver(receiver, filter);
}
#Override
protected void onStop() {
super.onPause();
unregisterReceiver(receiver);
}
private void activityMethod() {
}
}
You can start the Activity using an Intent and put a command code in the Intent extra fields. In your Activity you can then decide the behaviour based on the command code or resort to a default behaviour if none is present.
You can start an activity from your receiver via the normal means:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, YourActivity.class);
startActivity(i);
}
Note though that the user is going to expect that the phone application starts up since they are receiving a phone call. It is very likely a bad idea to hijack the phone call by dumping your own activity on top of the stock dialer app.

Categories

Resources