Feeds:
Posts
Comments

Archive for the ‘PendingIntent Flags’ Category

I was trying to google some resources on Pending Intent flags but couldn’t find a good explanation, so thought of writing the same. This blog explains the flags needed for creating Pending Intent.
The following example uses Pending Intent object for creating a notification.
What is Pending Intent– Pending Intent is used to grant other application the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
PendingIntent can be created as follows:
PendingIntent pi= PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
The above line of code retrieves a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
The last argument is the PendingIntent flag. The flags can take any of the following values:
FLAG_CANCEL_CURRENT -Flag for use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int): if the described PendingIntent already exists, the current one is canceled before generating a new one.
FLAG_NO_CREATE-Flag for use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int): if the described PendingIntent does not already exist, then simply return null instead of creating it.
FLAG_ONE_SHOT-Flag for use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int): this PendingIntent can only be used once.
FLAG_UPDATE_CURRENT-Flag for use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int): if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.
Example-The code below explains the concept of Pending Intent flags. The results obtained by running the same code with different flags ,are mentioned below :

package com.ls.la;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class NotificationProjActivity extends Activity {

	NotificationManager nm;
	Notification notification;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Intent notificationIntent = new Intent(this, MyClass.class);
		String n = Context.NOTIFICATION_SERVICE;
		nm = (NotificationManager) getSystemService(n);
		int icon = R.drawable.icon; // icon from resources
		String tickerText = "Hello"; // ticker-text
		long when = System.currentTimeMillis(); // notification time
		Context context = getApplicationContext(); // application Context
		CharSequence contentTitle = "My notification"; // expanded message title
		CharSequence contentText = "Hello World!"; // expanded message text

		notificationIntent.putExtra("n1", 30);
		notificationIntent.putExtra("n2", "test");

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		// the next two lines initialize the Notification, using the
		// configurations above
		notification = new Notification(icon, tickerText, when);
		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);
		notification.defaults = Notification.DEFAULT_SOUND
				| Notification.DEFAULT_VIBRATE;
		// long[] vibrate = { 0, 100, 200, 300 };
		// notification.vibrate = vibrate;
		notification.flags = Notification.FLAG_AUTO_CANCEL;

		nm.notify(1, notification);

		notificationIntent.putExtra("n1", 80);
		
		//creating 2nd Pending Intent with the matching intent and  		//using flag FLAG_NO_CREATE

		PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0,
				notificationIntent, PendingIntent.FLAG_NO_CREATE);

		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent1);
		notification.defaults = Notification.DEFAULT_SOUND
				| Notification.DEFAULT_VIBRATE;
		notification.flags = Notification.FLAG_AUTO_CANCEL;

		// for logging if the new pending intent created is same object or not

		if (contentIntent.equals(contentIntent1)) {

			Log.v("NotificationProjActivity", "OBJECTS ARE EQUAL");

		} else {
			Log.v("NotificationProjActivity", "OBJECTS ARE NOT EQUAL");

		}

		nm.notify(1, notification);

	
	}
}

In the above code a Pending Intent object contentIntent is created as follows:


 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

The notificationIntent is an Intent object having one integer extra and one String extra.


		notificationIntent.putExtra("n1", 30);
		notificationIntent.putExtra("n2", "test");

Then a second PendingIntent is created using the same matching Intent as follows:


PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0,
				notificationIntent, PendingIntent.FLAG_NO_CREATE);

However, a couple of points to mention here:
1. The same matching intent notificationIntent is used but the integer extra has been changed from 30 to 80.
notificationIntent.putExtra(“n1”, 80);
2. The PendingIntent flag FLAG_NO_CREATE is used for the 2nd PendingIntent contentIntent1.
Lastly, a log statement has been used in the code to check if the PendingIntent contentIntent and PendingIntent contentIntent1 are equal or not.


if (contentIntent.equals(contentIntent1)) {

			Log.v("NotificationProjActivity", "OBJECTS ARE EQUAL");

		} else {
			Log.v("NotificationProjActivity", "OBJECTS ARE NOT EQUAL");

		}

FLAG_NO_CREATE-
If, we run the above code, we will see that the Log message will print :
OBJECTS ARE EQUAL.
Also, when we retrieve the intent in MyClass (which will happen when we click on the notification from the notification tray), the values of integer extra will be 30 and not 80.
The above clearly explains that if there is an existing PendingIntent object which matches and the FLAG_NO_CREATE is specified then a factory method will return a PendingIntent instance which refers to the existing PendingIntent object.If this flag is specified a new PendingIntent instance will not be created.This can be verified by the log message and the integer extra.

FLAG_UPDATE_CURRENT-
Now run the above code, by changing the flag from FLAG_NO_CREATE to FLAG_UPDATE_CURRENT
When we run the code after changing the flag, we will see that the Log message will print :
OBJECTS ARE EQUAL.
Also, when we retrieve the intent in MyClass (which will happen when we click on the notification from the notification tray), the values of integer extra will be 80 and not 30.If FLAG_UPDATE_CURRENT flag is specified and a matching PendingIntent exists then it will be updated with the new extra value if any. This can be verified by the log message and the integer extra.

FLAG_CANCEL_CURRENT-
Now run the above Now run the above code, by changing the flag from FLAG_UPDATE_CURRENT to FLAG_CANCEL_CURRENT
When we run the code after changing the flag, we will see that the Log message will print :
OBJECTS ARE NOT EQUAL.
Also, when we retrieve the intent in MyClass (which will happen when we click on the notification from the notification tray), the values of integer extra will be 80.If this flag is specified and a matching PendingIntent exists then it is cancelled.

I have posted the source code of the PendingIntent flags at :

Download source code (NotificationProj.rar)

au revoir. profiter le blog (Bye. Enjoy the blog)

Read Full Post »