android - 未设置为默认时使用我的应用程序进行 NFC 支付

标签 android nfc android-pay

您好,我正在开发用于 NFC 支付的安卓应用程序。

Android 设置中有一个选项可以使用打开的应用程序而不是默认应用程序。例如,当我将默认应用程序设置为 Android Pay 并在付款前打开我的应用程序时 - 我想使用我的应用程序而不是默认应用程序进行付款。见下图。

enter image description here

我对其进行了测试,但遗憾的是我使用 Android Pay 付款,而不是我在前台运行的应用。

我没有在文档中找到任何字样,如果我必须添加一些东西来 list ,注册一些东西等等......?

我的 list 中有带有 intent filtermeta dataservice,当应用程序设置为默认付款时,它正在运行正确:

<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>

添加apduservice xml内容:

<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>

最佳答案

所以我终于找到了解决办法!

你必须使用 setPreferredService(Activity, ComponentName)在您当前的前台 Activity 中。建议在 onResume() 中调用它并且不要忘记调用 unsetPreferredService(Activity)一旦 Activity 不在前台(onPause() 是调用它的好地方)。

还有一点: setPreferredService(Activity, ComponentName)unsetPreferredService(Activity)已添加到 API 21 中,因此请确保不要从旧 API 调用它们。

关于android - 未设置为默认时使用我的应用程序进行 NFC 支付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421461/

相关文章:

java - 使用 Google Map Api v2 的 Android onLocationChanged with Direction

android - 如何使用 Eddystone URL 发送通知?

android - 如何从 ACS122U NFC 获取 RFID 标签

android - "Unrecognized App"安卓支付接口(interface)

google-chrome-extension - 测试 Chrome 应用内付款

paypal - Visa 结帐集成

android - BasicNetwork.performRequest : Unexpected response code 403 for https://www. googleapis.com/youtube/v3/playlistItems?

java - 发送带有参数的解析云代码时出现 IllegalArgumentException

android - 自定义 (NFC) NDEF 格式

android - 在 Android 上清除 MifareClassic 扇区的最佳方法是什么?