【问题标题】:How to NFC scan without refresh in android?如何在不刷新Android的情况下进行NFC扫描?
【发布时间】:2020-01-20 07:47:27
【问题描述】:

我正在尝试在 android 中进行 NFC 标记。 我的活动在 onCreate() 中有一个 webview 及其设置的 url。 NFC标签时,可以正常获取NDEF信息,但不维护页面地址。

我该如何解决?

@Override
public void onCreate(Bundle savedInstanceBundle){
    super.onCreate(savedInstanceBundle);
    setContentView(R.layout.activity_test);
    this.webview = findViewbyId(R.id.webview);
    this.webview.loadUrl("https://www.abcblarblar.com/");
    this.nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}

@Override
public void onResume(){
    super.onResume();

    Intent intent = getIntent();

    if (NfcAdapter.ACTION_TAG_DISCOVERED == intent.getAction()) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        ...
    } 
}

manifest.xml

<activity
        android:name="com.xxx.xxx.xxx.NFCActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

【问题讨论】:

    标签: android webview tags nfc ndef


    【解决方案1】:

    我猜(因为您没有显示有关如何读取 NFC 标签的代码)您正在使用enableForegroundDispatch

    此方法会在 Android 操作系统读取 NFC 卡时暂停您的应用。

    如果是这个原因,有两种可能的解决方案:-

    1) 确保您的应用程序处理暂停并将其保存为 InstanceState(因为它可能会被杀死并重新启动),或者您正在 onResume 中执行导致此问题的操作。

    2) 使用enableReaderMode https://developer.android.com/reference/android/nfc/NfcAdapter#enableReaderMode(android.app.Activity,%20android.nfc.NfcAdapter.ReaderCallback,%20int,%20android.os.Bundle) 读取卡片,因为通过这种读取NFC 卡片的方式,您的应用程序不会暂停,而是在您的应用程序的单独线程中创建回调

    【讨论】:

    • 感谢@Andrew 我写了 enableForegroundDispatch() 并确认 URL 被维护。
    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多