Android PendingIntent Class

A PendingIntent encapsulates an Intent and an action that will be carried out when its send method is invoked. Since a PendingIntent is a pending intent, the action is normally an operation that will be invoked some time in the future, most probably by the system.

For example, a PendingIntent can be used to construct a Notification so that something can be made happen when the user touches the notification.

The action in a PendingIntent is one of several methods in the Context class, such as startActivity, startService or sendBroadcast.

You have learned that to start an activity you can pass an Intent to the startActivity method on a Context.

Intent intent = …
context.startActivity(intent);

The equivalent code for starting an activity using a PendingIntent looks like this:

Intent intent = …
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
pendingIntent.send();

 The static method getActivity is one of several methods that returns an instance of PendingIntent. Other methods are getActivities, getService and getBroadcast.

These methods determine the action that the resulting PendingIntent can perform. Constructing a PendingIntent by calling getActivity returns an instance that can start an activity. Creating a PendingIntent using getService gives you an instance that can be used to start a service. You call getBroadcast if you want a PendingIntent for sending a broadcast.

To publish a notification, use the NotificationManager, which is one of the built-in services in the Android system. As it is an existing system service, you can obtain it by calling the getSystemService method on an activity, like so:

NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

Then, you can publish a notification by calling the notify method on the NotificationManager, passing a unique ID and the notification.

notificationManager.notify(notificationId, notification); 

The notification ID is an integer that you can choose. This ID is needed just in case you want to cancel the notification, in which case you pass the ID to the cancel method of the

NotificationManager:

notificationManager.cancel(notificationId);

The NotificationDemo project is an application that shows how to use notifications. The main activity of the app contains two buttons, one for publishing a notification and one for cancelling it. After the notification is published, opening it will invoke a second activity.

The layout file of the main activity of NotificationDemo

 <LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:onClick=“setNotification”
android:text=“Set Notification” />
<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:onClick=“clearNotification”
android:text=“Clear Notification” />
</LinearLayout>

 

 The main activity class

package com.example.notificationdemo;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

public class MainActivity extends Activity {

int notificationId = 1001;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

public void setNotification(View view) {

Intent intent = new Intent(this, SecondActivity.class);

PendingIntent pendingIntent =

PendingIntent.getActivity(this, 0, intent, 0);

Notification notification = new Notification.Builder(this)

.setContentTitle(“New notification”)

.setContentText(“You’ve got a notification!”)

.setSmallIcon(android.R.drawable.star_on)

.setContentIntent(pendingIntent)

.setAutoCancel(true)

.addAction(android.R.drawable.ic_menu_gallery,

“Open”, pendingIntent)

.build();

NotificationManager notificationManager =

(NotificationManager) getSystemService(

NOTIFICATION_SERVICE);

notificationManager.notify(notificationId, notification);

}

public void clearNotification(View view) {

NotificationManager notificationManager =

(NotificationManager) getSystemService(

NOTIFICATION_SERVICE);

notificationManager.cancel(notificationId);

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *