Programatically set NFC_PAYMENT_FOREGROUND setting for HCE in Android

1.3k views Asked by At

I am trying to set priority for a payment card in a foreground activity, but setPreferedPaymentService() won't work until categoryAllowsForegroundPreference() returns true. However, this is tied to the "Foreground priority" (NFC_PAYMENT_FOREGROUND) system setting.

Has someone encountered a similar problem? How can I change that setting programmatically?

1

There are 1 answers

0
Michael Roland On

The whole point of the the setting "Foreground priority: Use payment app in foreground, even over default" (Settings.Secure.NFC_PAYMENT_FOREGROUND) is to give the device user an option to prevent apps from doing exactly this. Consequently, if there was a programmatic way to re-enable this option (after the user intentionally disabled it), having this option would be pointless.

Therefore, the answer is: You can't programmatically enable that system setting from your app.

However, what you can do is ask the user to reconsider their choice. You could do this, for instance, by sending a CardEmulation.ACTION_CHANGE_DEFAULT intent to let the user select your app as the default. Or you could navigate the user to the Tap & pay settings (using a Settings.ACTION_NFC_PAYMENT_SETTINGS intent).