HelloCW
HelloCW

Reputation: 2325

Can I set flags as 0 in PendingIntent.getService

The Code 1 and Code 2 are create a pendingIntent object, but in some samples code it writed as Code 1 , and in some other samples code it writed as Code 2. Which one is correct? Thanks!

Code 1

pendingIntent = PendingIntent.getService(mContext,
                0,
                new Intent(mContext, CleanupService.class),
                PendingIntent.FLAG_CANCEL_CURRENT);

Code 2

 pendingIntent = PendingIntent.getService(mContext,
                0,
                new Intent(mContext, CleanupService.class),
                0);

Upvotes: 1

Views: 1732

Answers (1)

tachyonflux
tachyonflux

Reputation: 20211

A flag basically represents a single bit of information in an int, which is why their values are always powers of 2. And why you can set multiple flags with a bitwise or:

pendingIntent = PendingIntent.getService(mContext,
    0,
    new Intent(mContext, CleanupService.class),
    PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_NO_CREATE);

Your two code blocks do different things and neither is more "correct" than the other.

FLAG_CANCEL_CURRENT will basically cancel all existing pending intents that would launch an equivalent intent

0 corresponds to all flags off

Upvotes: 3

Related Questions