Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are different between PendingIntent.FLAG_UPDATE_CURRENT and 0 in android?

Tags:

android

What are different between PendingIntent.FLAG_UPDATE_CURRENT and 0 in android?

var aa=  PendingIntent.getService(mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)

var bb=  PendingIntent.getService(mContext, 0, myIntent, 0)
like image 861
HelloCW Avatar asked May 02 '18 06:05

HelloCW


People also ask

What is PendingIntent FLAG_UPDATE_CURRENT?

int. FLAG_UPDATE_CURRENT. Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

What is the difference between intent and PendingIntent?

In conclusion, the general and main difference between Intent and PendingIntent is that by using the first, you want to start / launch / execute something NOW, while by using the second entity you want to execute that something in the future.

What is PendingIntent Flag_immutable?

FLAG_IMMUTABLE : Indicates the Intent inside the PendingIntent cannot be modified by other apps that pass an Intent to PendingIntent.send() . An app can always use FLAG_UPDATE_CURRENT to modify its own PendingIntents. Prior to Android 12, a PendingIntent created without this flag was mutable by default.

Why would you use a PendingIntent?

A Pending Intent specifies an action to take in the future. It lets you pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as your application, whether or not your application is still around when the Intent is eventually invoked.


3 Answers

Setting PendingIntent.FLAG_UPDATE_CURRENT

Based on the documentation for PendingIntent.FLAG_UPDATE_CURRENT:

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

Usage

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

Setting Value to 0

Setting no flags, i.e. 0 as the flags parameter, is to return an existing PendingIntent if there is one that matches the parameters provided. If there is no existing matching PendingIntent then a new one will be created and returned

like image 144
Sagar Avatar answered Oct 07 '22 17:10

Sagar


If you are passing 0 as a flag you are saying that you are passing no flags to that PendingIntent

There are a difference, at least with the number.

See what is that current constant

public static final int FLAG_UPDATE_CURRENT = 134217728;

From docs FLAG_UPDATE_CURRENT is :

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

For more information about this issue check this answer

like image 25
Skizo-ozᴉʞS Avatar answered Oct 07 '22 17:10

Skizo-ozᴉʞS


For passing 0:

When you call:

PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);

passing 0 as the flags parameter means that you are setting no flags.


If you call:

PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);

again, and the Intent you pass matches the Intent from the first call, then you will get back the same PendingIntent as from the first call. "matches" means that the ACTION, DATA, CATEGORY, and COMPONENT are all the same. Extras are not considered when matching.

If you provide different extras in the Intent for the second call, those extras will NOT be present in the PendingIntent when it is sent. The extras in the Intent from the first call will be used.

For passing PendingIntent.FLAG_UPDATE_CURRENT:

if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

like image 26
Rahul Avatar answered Oct 07 '22 17:10

Rahul