Skip to main content

Broadcast receivers in Android - ABNHive

It responds to system-wide broadcast announcements. It used to display a persistent icon in the status bar of the device, vibrate, play a sound, flashlights, etc. to indicate to the user that something significant has happened in the background.


System Generated Events

The following table lists a few important system events.
  • android.intent.action.BATTERY_CHANGED - Sticky broadcast containing the charging state, level, and other information about the battery.
  • android.intent.action.BATTERY_LOW - Indicates low battery condition on the device.
  • android.intent.action.BATTERY_OKAY - Indicates the battery is now okay after being low.
  • android.intent.action.DATE_CHANGED - The date has changed.
  • android.intent.action.REBOOT - Have the device reboot.
  • android.intent.action.BOOT_COMPLETED - This is broadcast once after the system has finished booting.
  • android.intent.action.BUG_REPORT - Show activity for reporting a bug.
  • android.intent.action.CALL - Perform a call to someone specified by the data.
  • android.intent.action.CALL_BUTTON - The user pressed the "call" button to go to the dialer or other appropriate UI for placing a call.

Broadcast Receiver Example

In this sample application, a notification is generated when you change the system time. The notification when clicked leads the user to the Contacts. This is how the application works:

public class MyBroadcastReceiver extends BroadcastReceiver {
 
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
 
    @Override
    public void onReceive(Context context, Intent intent) {
 
        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
 
        Notification notifyDetails = new Notification(R.drawable.android,
                "Time Reset!", System.currentTimeMillis());
 
        PendingIntent myIntent = PendingIntent.getActivity(context, 0,
                new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0);
 
        notifyDetails.setLatestEventInfo(context, "Time has been Reset",
                "Click on me to view Contacts", myIntent);
 
        notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
        notifyDetails.flags |= Notification.DEFAULT_SOUND;
 
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
 
    }

- Aswin Bhim Nath

Comments

Popular posts from this blog

Android Application Components Activities, Services, Broadcast Recievers, Content Providers - ABNHive

Application components are the essential building blocks of Android app development.  Each component is an entry point through which the system or a user can enter your app. There are 4 types of components in android Activities Services Broadcast receivers Content providers Let's dive into the deep.  Activities This is a component that represents a single screen with a user interface or simply an entry point for interacting with the user. Example, an email app might have one activity that shows a list of new emails. Activities work together to form a cohesive user experience in the app. For example, clicking a button on the first screen might replace that screen with another screen containing other widgets, a web page, a movie, or text information. Activities are typically created by subclassing (extending) the Android class Activity.  click here   to know more about Activity. Services This runs in the background ...

An Introduction to Layouts in Android - ABNHive

There is a number of Layouts provided by Android which you will use in almost all the Android applications to provide a different view, look and feel. Linear Layout - LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. Linear layout is used to place one element on each line. So, all the elements will be placed in an orderly top-to-bottom fashion. Relative Layout - RelativeLayout is a view group that displays child views in relative positions. Using relative layout, we can specify the position of the elements in relation to other elements, or in relation to the parent container. Table Layout - TableLayout is a view that groups view into rows and columns.  Absolute Layout - In AbsoluteLayout we can specify the exact coordinates of each control that we want to place. In absolute layout, we will give the exact X and Y coordinates of each control. Frame Layout - It is used when you want to show one item on each sc...

Shortcuts in Android Studio for Android Developers - ABNHive

I hope these shortcuts will definitely help you to increase the productivity level. Search for command: cmd + shift + a ( Windows / Linux : ctrl +  shift + a) You just type: close and you’ll get a proper shortcut/command. Choose from the last copy / pastes : cmd +  shift  + v ( Windows / Linux : Ctrl + Shift + v). By default, there are 5 last copy/paste items. Enable mulmulti cursorature: ctrl + g (alt + j for Windows / Linux ). Open a class: cmd + o ( Windows / Linux : ctrl + n). Open any file: cmd + shift + o ( Windows / Linux : ctrl + shift + n) Open symbol:  cmd + option + o ( Windows / Linux : alt + shift + n). Go to implementation: cmd + option + b ( Windows / Linux : ctrl + alt + b). Go to declaration: cmd + b ( Windows / Linux : ctrl + b).  Go to type declaration: control + shift + b ( Windows / Linux : ctrl + shift + b).  Go to super: cmd + u ( Windows / Linux : ctrl + u).  Move between tabs: cmd + shift + [ (move left) o...