Broadcast receiver and notification

Android training course is designed to impart thorough knowledge on Android and its features. It includes Android Architecture, Android Components, Application Lifecycle process, Android Resources, Layout Manager and Styles, Intents, Menus, Service Lifecycle, Broadcaster receiver and Notification, Preference Management, SQLite Database, Google Maps, Location Tracking, Google Play Registration.
In this tutorial, you will learn how to implement a Broadcast Receiver in your Android application. Broadcast Receiver is completely asynchronous, and is considered to be a foreground process and will be kept running with the Android system. So lets begin…
Broadcast receiver

It is an android application component, which can respond to broadcast message from any another application or system itself. For example:
- battery low
- SMS receiving
- Wifi activated / deactivated message etc.
Kind of a job to pass notification, when specified event occur.

Registration of Broadcast Receiver
Static: Use <receiver> tag in your manifest file.
Dynamic: Use context.registerreceiver()method in code.
Broadcasting the message using send broadcast() method:
//Create an intent with an action
String uniqueActionString=”com.androidbook.intents.testbc”;
Intent broadcast intent=new intent(unique action string);
Broadcastintent.putextra(“message”,”Hello world”);
Activity.send broadcast(broadcastintent);
Listen to broadcast message
Public class receiver extends broadcastreceiver
{
Private static final string tag=”Receiver”;
@Override
public void on receive(Context context,Intent intent)
{
Log.d(“Test Receiver”,”intent=”+intent);
String message=intent.getStringExtra(“message”);
Log.d (tag,message);
}
}
Registering a receiver in the manifest file
<manifest>
<application>
…
<activity…>
…
<reciverandroid:name=”Receiver”>
<intent-filter>
<action
android:name =”com.androidbook.intents.testbc”/>
</intent-filter>
</receiver>
…
</application>
</manifest>
Notification

Kind of a messagethat can be display in notification area of your phone.
To see of the notifications,the user opens the notification drawer.
Creating Notifications
NotificationCompat.Buildernbuilder=new
NotificationCompat.Builder(this);
nbuilder.setSmallicon(R.drawable.img);
nbuilder.setContentTitle(“new msg”);
nbuild.setContextText(“hello everyone”);
Context Menu
Show notificationto the user using NotificationManager
NotificationManager nm=(NotificationManager)
Getsystemservice(context.Notification_Service);
Nm.notify(System.currentTimemillis(),mn.build());