如何从NFC标签获取纯文本? [英] How to get plain text from NFC Tag?

查看:97
本文介绍了如何从NFC标签获取纯文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试实现Google文档中的代码,但是对我来说仍然不清楚.我在这里想念什么?

I have tried to implement the code from Google documentation but it is still not clear for me. What am I missing here?

这是MainActivity.java中的方法:

This is method in MainActivity.java:

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMessages =
                intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMessages != null) {
            NdefMessage[] messages = new NdefMessage[rawMessages.length];
            for (int i = 0; i < rawMessages.length; i++) {
                messages[i] = (NdefMessage) rawMessages[i];
                Log.i("nfcccc",messages[i].toString());
            }
            // Process the messages array.
            Log.i("from tag: ",messages.toString());
            Toast.makeText(this,messages.toString(),Toast.LENGTH_LONG).show();

        }
    }
}

在onCreated方法中,我还初始化了nfcAdapter:

In onCreated method I have also initialized nfcAdapter:

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

清单文件中我有这个:

<activity android:name=".MainActivity">

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" />
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>



    </activity>

我花了几天的时间来尝试实现它,但是它仍然无法正常工作.我正在做的是使用NFC Tools android应用程序保存纯文本,并且我想在自己的应用程序中读取此数据>

I spent couple of days trying to implement that, but it is still not working. What I am doing is that I use NFC Tools android app to save plain text and the I want to read this data in my own app>

推荐答案

您缺少的是与Activity的启动模式的交互,因为NFC处理是由另一个系统应用程序完成的,然后是与Google文档作为如何使用NFC的交互就像您的应用程序正在从另一个应用程序接收数据一样.

What you are missing is the interaction with the Activity's launch mode as NFC handling is done by another System App and then with what Google document as how to use NFC it is just like your application is receiving data from another App.

所以 onNewIntent

对于将launchMode设置为"singleTop"的活动进行调用.在他们的包裹中

This is called for activities that set launchMode to "singleTop" in their package

https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)

您的应用程序默认将启动模式设置为 standard ,因此永远不会调用 onNewIntent

Your application will have launchMode as standard as default therefore onNewIntent is never called

将2个标准NFC API与Intents一起使用

There are 2 standard parts for using the old NFC API's with Intents

  1. 活动未运行(通常意味着App Task未运行),在活动"中设置清单过滤器,然后在 oncreate 中使用 getIntent ,然后进行处理成为基于NFC的Intent(按照您在 onNewIntent 上所做的操作)

  1. Activity is not running (which usually means the App Task is not running), set manifest filters in Activity and then in oncreate use getIntent and then process the Intent for being an NFC based Intent (as per you would do on onNewIntent)

活动(和应用程序)正在运行,并且在前台, enableForegroundDispatch onNewIntent (这对暂停和恢复活动具有负面影响)

Activity (and App) is running and in the foreground, enableForegroundDispatch https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#foreground-dispatch which basically tells the NFC system app to restart this Activity when it is the "Top" activity as though it was a "singleTop" Activity, then onNewIntent is called by the incoming Intent (This has a negative side effect of Pausing and Resuming your Activity)

请注意,可以使用不同的方法来设置launchMode以及将堆栈发送到 onNewIntent 的意图绑定到栈顶部的Activity,但这并不常见.
最好在 onCreate enableForegroundDispatch 中处理NFC Intent,并在 onNewIntent

Note there are different ways to setup launchMode and what Activity is top of the stack to get it to tie up sending the Intent to onNewIntent but they are less common.
Best to handle NFC Intents in onCreate and alsoenableForegroundDispatch with the same handling in onNewIntent

获得更多控制权的更好方法是使用更新更好的 enableReaderMode API

An even better way to get more control is use the newer and better enableReaderMode API https://developer.android.com/reference/android/nfc/NfcAdapter#enableReaderMode(android.app.Activity,%20android.nfc.NfcAdapter.ReaderCallback,%20int,%20android.os.Bundle)

摘要因此,只需按照 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆