Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf ·...

17
Broadcast Receiver

Transcript of Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf ·...

Page 1: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Broadcast Receiver

Page 2: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Why do we need Broadcast Receiver?

Page 3: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Broadcast Receivers• Broadcast receiver (a BroadcastReceiver subclass) is one

of the application's components. Broadcast receivers enable applications to receive intents that are broadcasted by the system or by other applications and respond to broadcast messages from other applications or from the system itself.

• A system broadcast can announce that the screen has turned off, the battery is low, or a picture was captured. A custom broadcast initiated by app can inform other app that some data has been downloaded to the device and is available for them to use.

• Broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. Most of time, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work such as initiate a service to do some work based on the event.

Page 4: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Broadcast registration

• The broadcast messages are sometime called eventsor intents. Broadcast receiver will intercept this communication and will initiate appropriate action. Of course, the broadcast receiver must register with the interested event in advance.

• There are two ways to make a broadcast receiver known to the system: – One is to declare it in the manifest file with this element. – The other is to create the receiver dynamically in code and

register it with the registerReceiver() method.

• Two important steps to make BroadcastReceiverworks for the system broadcasted intents:

Creating the Broadcast Receiver.Registering Broadcast Receiver

Page 5: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

1. Creating the Broadcast Receiver

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive method where each message is received as a Intent object parameter.

public class MyReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();

}

}

Page 6: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

2. Registering Broadcast Receiver

• An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file. eg. to register MyReceiverfor system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.

Page 7: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Broadcast-Receiver registration<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<receiver android:name="MyReceiver">

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED">

</action>

</intent-filter>

</receiver>

</application>

Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver MyReceiver and implemented logic inside onReceive will be executed

Page 8: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Samples of system Event Constant

• android.intent.action.BATTERY_CHANGED

• android.intent.action.BATTERY_LOW

• android.intent.action.BATTERY_OKAY

• android.intent.action.BOOT_COMPLETED

• android.intent.action.BUG_REPORT

• android.intent.action.CALL

• android.intent.action.CALL_BUTTON

• android.intent.action.DATE_CHANGED

• android.intent.action.REBOOT

Page 9: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Broadcasting Custom Intents

Page 10: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast
Page 11: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast
Page 12: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Broadcasting Custom Intents

• If you want your application itself should generate and send custom intents then you will have to create and send those intents by using the sendBroadcastmethod inside your activity class.

public void broadcastIntent(View view)

{

Intent intent = new Intent();

intent.setAction("com.tutorialspoint.C

USTOM_INTENT");

sendBroadcast(intent);

}

Page 13: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

CUSTOM INTENT registration• This intent com.tutorialspoint.CUSTOM_INTENT can also be registered in similar way as we have

regsitered system generated 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.tutorialspoint.CUSTOM_INTENT">

</action>

</intent-filter>

</receiver>

</application>

Page 14: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Ordered Broadcast Receiver

Page 15: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast
Page 16: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

Summary

• Broadcast Receiver runs on main thread, can lead to ANR errors

– Android recommends less than 10 seconds

• BR is not suitable for asynchronous calles

• Never bind to a service from broadcast receiver

• BR are prone to hacks, thus always secure them– Set exported attributes to false

– Use LocalBroadcastManager to limit intent propagation beyond app

Page 17: Broadcast Receiver - Kennesaw State Universityksuweb.kennesaw.edu/.../06Broadcast_Receivers.pdf · 2. Registering Broadcast Receiver •An application listens for specific broadcast

AsyncTask