Saturday 20 September 2014

Android: Sending broadcast message within Application and Across the application.


Adding demo here to send broadcast message within application and across the application.


  • BroadcastReceiver
  • Sending bundle to BroadcastReceiver
  • sendStickyBroadcast
  • Calling actvity method from BroadcastReceiver

We are going to add two project one for sending broadcast to its own and also to other app and other one will only receive broadcast.
  • SendBroadcast
  • ReceiveBroadcast


We can register broadcast in manifest and also in activty class. We are going to register broadcast in onResume method of activity. Because we are going to call public method of this activity from broadcast receiver. 


        @Override
protected void onResume() {
/**
* Registering only this two BroadcastReceiver so that we will get update for only this two                      broadcast. 
* */
IntentFilter toOwnAppFilter = new IntentFilter("com.send.broadcast.ToOwnApp");
toOwnAppReceiver = new BroadcastReceiverToOwnApp();
registerReceiver(toOwnAppReceiver, toOwnAppFilter);
IntentFilter toBothAppFilter = new IntentFilter("com.send.broadcast.ToBothApp");
toBothAppReceiver = new BroadcastReceiverToBothApp();
registerReceiver(toBothAppReceiver, toBothAppFilter);
super.onResume();
}

We have to unregister this broadcast in pause method.

@Override
protected void onPause() {
/**
* Unregister broadcast receiver when activity pause 
* */
unregisterReceiver(toOwnAppReceiver);
unregisterReceiver(toBothAppReceiver);
super.onPause();
}


Full Activity Code is Below:::::::::::::::::::::

package com.send.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * @author dipenp
 *
 */
public class SendBroadcastActivity extends Activity {

BroadcastReceiverToOwnApp toOwnAppReceiver;
BroadcastReceiverToBothApp toBothAppReceiver;
public static String EXTRA = "extra";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_broadcast);
findViewById(R.id.sendBroadcastToOwnApp).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.send.broadcast.ToOwnApp");
intent.putExtra(EXTRA, "To Own App");
// sendStickyBroadcast(intent);
sendBroadcast(intent);
}
});
findViewById(R.id.sendBroadcastToOtherApp).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.ToOtherApp");
intent.putExtra(EXTRA, "To Other App");
sendStickyBroadcast(intent);//Sending sticky intent so that Receiver app can get this intent when it will register this BroadcastReceiver
// sendBroadcast(intent);
}
});
findViewById(R.id.sendBroadcastToBothApp).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.send.broadcast.ToBothApp");
intent.putExtra(EXTRA, "To Both App");
sendStickyBroadcast(intent);//Sending sticky intent so that Receiver app can get this intent when it will register this BroadcastReceiver
// sendBroadcast(intent);
}
});
}

@Override
protected void onResume() {
/**
* Registering only this two BroadcastReceiver so that we will get update for only this two broadcast. 
* */
IntentFilter toOwnAppFilter = new IntentFilter("com.send.broadcast.ToOwnApp");
toOwnAppReceiver = new BroadcastReceiverToOwnApp();
registerReceiver(toOwnAppReceiver, toOwnAppFilter);
IntentFilter toBothAppFilter = new IntentFilter("com.send.broadcast.ToBothApp");
toBothAppReceiver = new BroadcastReceiverToBothApp();
registerReceiver(toBothAppReceiver, toBothAppFilter);
super.onResume();
}
@Override
protected void onPause() {
/**
* Unregister broadcast receiver when activity pause 
* */
unregisterReceiver(toOwnAppReceiver);
unregisterReceiver(toBothAppReceiver);
super.onPause();
}

/**
* Adding public method which we will call from broadcast receiver class.
* It is possible only when we will register broadcast from this activity. 
*/
public void makeToast() {
Toast.makeText(getApplicationContext(), "Calling activity from Broadcast.", Toast.LENGTH_LONG).show();
}
}

Broadcast Receiver Code is Below:::::::::::

package com.send.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * @author dipenp
 *
 */
public class BroadcastReceiverToOwnApp extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "BroadcastReceiverToOwnApp :: "+arg1.getExtras().getString(SendBroadcastActivity.EXTRA), Toast.LENGTH_LONG).show();
/**
* Calling activity method from broadcast receiver.
* Only possible if this broadcast is register from this activity.*/
try {
((SendBroadcastActivity)arg0).makeToast();
} catch (ClassCastException e) {
}
}
}

You can download demo code from below urls:
SendBroadcast: 

ReceiveBroadcast: