Hi I'm working on android application for NFC payments.
There is an option in Android settings to use an open application instead of the default one. For example when I have default application set to Android Pay and I open my app before the payment - I want to use my app for the payment instead of the default one. See image bellow.
I tested it, but unfortunately I paid with Android Pay instead of my app that was running in foreground.
I did not find any word in documentation, if I have to add something to manifest, register something etc..?
I have service
with intent filter
and meta data
in my manifest and when the app is set as default for payment, it is working correctly:
<service
android:name="com.example.MyWalletHceService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/>
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/apduservice"/>
</service>
Adding apduservice xml content:
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:apduServiceBanner="@drawable/ic_logo_nfc_system"
android:description="@string/nfc_service_title"
android:requireDeviceUnlock="false">
<aid-group
android:category="payment"
android:description="@string/nfc_aid_desc">
<!-- Following is a PPSE AID. You must always include this AID in order for payments to
work-->
<aid-filter android:name="@string/aid_number"/>
<!-- Following AID list is specific to the application requirements.
If your application supports the type of card that these AID represents,
you must include those AID in your configuration file -->
<aid-filter android:name="@string/aid_mc_filter"/> <!-- Mastercard DEBIT/CREDIT -->
</aid-group>
So I finally found a solution!
You have to use setPreferredService(Activity, ComponentName) in your current foreground activity. It is recommended to to call it inside onResume()
and also do not forget to call unsetPreferredService(Activity) once the activity is not in foreground (onPause()
is good place to call it).
One more thing: setPreferredService(Activity, ComponentName) and unsetPreferredService(Activity) were added in API 21, so be sure not to call them from older API.