[android] Differentiate requestCode for PendingIntent #9198

Closed
RicoElectrico wants to merge 1 commit from mb-PendingIntent-requestCode-fix into master
2 changed files with 4 additions and 3 deletions

View file

@ -83,7 +83,7 @@ public class TrackRecordingService extends Service implements LocationListener
final int FLAG_IMMUTABLE = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? 0 : PendingIntent.FLAG_IMMUTABLE;
final Intent contentIntent = new Intent(context, MwmActivity.class);
mPendingIntent = PendingIntent.getActivity(context, 0, contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT | FLAG_IMMUTABLE);
PendingIntent.FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
return mPendingIntent;
}

View file

@ -46,6 +46,7 @@ public class NavigationService extends Service implements LocationListener
{
private static final String TAG = NavigationService.class.getSimpleName();
private static final String STOP_NAVIGATION = "STOP_NAVIGATION";
private static final int REQ_CODE_STOP_NAVIGATION = 1;
biodranik commented 2024-09-02 22:13:21 +00:00 (Migrated from github.com)
Review

Should these codes be unique compared to MwmActivity ones? If yes, then it would be safer to keep all such codes in a single file/place, to avoid accidentally reusing the same code for different notifications. WDYT?

Should these codes be unique compared to MwmActivity ones? If yes, then it would be safer to keep all such codes in a single file/place, to avoid accidentally reusing the same code for different notifications. WDYT?
RicoElectrico commented 2024-09-02 22:29:33 +00:00 (Migrated from github.com)
Review

I found another cleaner way to deal with the issue. As all the pending intents do the same thing, one can use FLAG_UPDATE_CURRENT which will not cancel the pending intent from the previous invocation. As a result all the services hold a reference to the same pending intent doing the desired thing i.e. opening OM in the foreground.
I think we need an Android expert to comment on what is considered best practice.

I found another cleaner way to deal with the issue. As all the pending intents do the same thing, one can use FLAG_UPDATE_CURRENT which will not cancel the pending intent from the previous invocation. As a result all the services hold a reference to the same pending intent doing the desired thing i.e. opening OM in the foreground. I think we need an Android expert to comment on what is considered best practice.
kavikhalique commented 2024-09-05 22:09:00 +00:00 (Migrated from github.com)
Review

I will go with FLAG_UPDATE_CURRENT

As android suggests to use FLAG_CANCEL_CURRENT only if you want to invalidate all other instances of pending intent by any service but here our logic is not to invalidate previous instances and also using it will remove the problem of similar pending intent creation by mistake.

I will go with FLAG_UPDATE_CURRENT As android suggests to use FLAG_CANCEL_CURRENT only if you want to invalidate all other instances of pending intent by any service but here our logic is not to invalidate previous instances and also using it will remove the problem of similar pending intent creation by mistake.
private static final String CHANNEL_ID = "NAVIGATION";
private static final int NOTIFICATION_ID = 12345678;
@ -138,11 +139,11 @@ public class NavigationService extends Service implements LocationListener
final int FLAG_IMMUTABLE = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? 0 : PendingIntent.FLAG_IMMUTABLE;
final Intent contentIntent = new Intent(context, MwmActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT | FLAG_IMMUTABLE);
PendingIntent.FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
final Intent exitIntent = new Intent(context, NavigationService.class);
exitIntent.setAction(STOP_NAVIGATION);
final PendingIntent exitPendingIntent = PendingIntent.getService(context, 0, exitIntent,
final PendingIntent exitPendingIntent = PendingIntent.getService(context, REQ_CODE_STOP_NAVIGATION, exitIntent,
PendingIntent.FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE);
mNotificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)