Android 如何从NFC标签获取纯文本?

Android 如何从NFC标签获取纯文本?,android,android-studio,nfc,reader,Android,Android Studio,Nfc,Reader,我试图实现谷歌文档中的代码,但我仍然不清楚。我错过了什么 这是MainActivity.java中的方法: @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { Parcelable[] rawMessages =

我试图实现谷歌文档中的代码,但我仍然不清楚。我错过了什么

这是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();

        }
    }
}
在清单文件中,我有以下内容:

<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工具android应用程序保存纯文本,我想在我自己的应用程序中读取这些数据>

您缺少的是与活动启动模式的交互,因为NFC处理是由另一个系统应用程序完成的,然后是谷歌文档中关于如何使用NFC的内容,就像您的应用程序一样从另一个应用程序接收数据

所以
onNewIntent

对于在其包中将launchMode设置为“singleTop”的活动,会调用此命令

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

有两个标准部件用于有意使用旧的NFC API

  • 活动未运行(这通常意味着应用程序任务未运行),在活动中设置清单筛选器,然后在
    oncreate
    中使用
    getIntent
    ,然后处理基于NFC的意图(按照您在
    onNewIntent
    上的操作)

  • 活动(和应用程序)正在运行,并且在前台,
    enableForegroundDispatch
    基本上告诉NFC系统应用程序在“顶级”活动时重新启动此活动,就像它是一个“单顶”活动一样,然后传入的意图调用
    onNewIntent
    (这会对暂停和恢复活动产生负面影响)

  • 注意:有不同的方法来设置launchMode,堆栈顶部是什么活动,以便将意图发送到
    onNewIntent
    ,但它们不太常见。
    最好在
    onCreate
    中处理NFC意图,并在
    onNewIntent

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

    摘要
    因此,只需根据

    非常感谢!:)添加
    foregroundDispatch
    的代码即可,但我在保存字符串之前获得了垃圾数据。例如:如果标记包含字符串“Hello”,我将得到“\TenHello”。你知道有什么问题吗?也许我应该只提取字节数组的一部分,然后将其转换为字符串?文本记录是用语言编码的参见我的另一个答案
    <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>