Sending Broadcasts

Android Provides three ways to send broadcast.

1.Normal Broadcasts
2.Ordered Broadcasts
3.LocalBroadcastManager.sendBroadcast

1.Normal Broadcasts

  1.This is done by using sendBroadcast(intent)
2.Sends broadcast to all receivers without order.
3.This is efficient.But receivers cannot use result from other receiver.

2.Ordered Broadcasts

1.Send by using sendOrderedBroadcast(Intent,String)
2.It Sends broadcast to only one receiver at a time.
3.Order is specified using android:priority attribute
4.Receivers with same priority will run in Random Order.
5.Can propagate result from one receiver to another receiver.

3.LocalBroadcastManager.sendBroadcast

1.Send broadcast to receiver which is in same app.
2.If you dont want to send broadcast across apps you have to go for LocalBroadcastManager.sendBroadcast
3.Advantage of this is,you dont have to worry about security issues related to other apps being able to receiver broadcast.

Advertisements

Broadcast Custom Intent

How to  Broadcast Custom  Intent?

If you want your application should  generate and send custom intent,use

sendBroadcast();
sendStickyBroadcast();

How to Create Custom Intent?

   Intent intent = new Intent();
   intent.setAction("com.wordpress.myandroidninja.CUSTOM_INTENT");
   sendBroadcast(intent);


How to Register Custom Intent?

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
   <intent-filter>
         <action android:name="com.wordpress.myandroidninja.CUSTOM_INTENT">
         </action>
      </intent-filter>
   </receiver>
</application>
 

Pending Intent

What is Pending Intent?

Pending intent is an object that wraps another intent object with the target action to be performed.

It can be used to launch an activity,service,Broadcast receiver.

It is passed to other application to perform desired actions.

Types of Pending Intent?

getActivity(Context,int,Intent ,int);
getBroadcast(Context,int,Intent ,int);
getService(Context,int,Intent ,int);

Arguments of Pending Intent?

this(Context) – the context in which the PendingIntent starts the activity.

requestCode – a private request code for the sender.

intent – An explicit intent of the activity to be launched.

PendingIntent.FLAG_CANCEL_CURRENT – If described PendingIntent already exists,then cancel first once,and n new pending Intent is generated.

Broadcast Receiver

What is broadcast receiver?

It is an Application component.
Allows you to register for system or application events.
All registered receiver for an events are notified by android runtime once this event happens.
The broadcast can be generated either by system or by other application.
App can listen important System events such as Low battery,new Messages,System Reboot.

How to register broadcast receiver?

1.Statically – Declare in manifest file.
1.1.Broadcast receiver is declared in manifest.xml file like other components of android
1.2.Can’t be Unregistered.

2.Dynamically – In Java code
2.1.Broadcast receiver is registered using register() method.
2.2.Can Unregistered later.

How to use broadcast receiver?

1.Create broad cast receiver.

1.1.Broadcast receiver extend BroadcastReceiver class.
1.2.Override onReceive() method by default.

 public class MyReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
 }
 }

2.Register broad cast receiver.
2.1.An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file.
2.2.Register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">
 <action android:name="android.intent.action.BOOT_COMPLETED">

Registering BroadcastReceiver Programatically

1.Instead of creating broadcast receiver in Manifest,you can do directly in Code using IntentFilter Class.

IntentFilter filter = new IntentFilter("com.myandroidninja.BroadcastReceiver");
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver, filter);

2.Registering Broadcast receiver will happen in onResume()
3.Once you register broadcast receiver,It is highly recommended to Unregister that in onPause()

@Override
protected void onPause() {
    unregisterReceiver(mReceiver);
    super.onPause();
}