【问题标题】:Android: Receiving and processing NDEF recordsAndroid:接收和处理 NDEF 记录
【发布时间】:2014-04-12 20:03:22
【问题描述】:

我一直在尝试做的是发送一个整数并接收它,然后将该整数设置为正在倒计时的计时器。到目前为止,我能够发送整数并在另一台设备上打开应用程序,但是当设备加载活动时,它会打开 MainActivity 而不是 Newgame 活动。在这一点上我必须承认我的代码并不聪明,而且有点新手,但这里是处理 NFC 通信的代码摘录,摘录来自 Newgame.java:

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    int time = bomb1.getTimer();
    String message = ( " " + time);
    NdefMessage msg = new NdefMessage(
            new NdefRecord[] { NdefRecord.createMime(
                    "application/vnd.com.Jhadwin.passthebomb.newgame" ,message.getBytes())
                    ,NdefRecord.createApplicationRecord("com.Jhadwin.passthebomb")
    });
    return msg;
}    
@Override
public void onResume() {
    super.onResume();
    // Check to see that the Activity started due to an Android Beam
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
    }
}

@Override
public void onNewIntent(Intent intent) {
    // onResume gets called after this to handle the intent
    setIntent(intent);
}

/**
 * Parses the NDEF Message from the intent and prints to the TextView
 */
 void processIntent(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    String newtimermsg = new String(msg.getRecords()[0].getPayload());
    timeremtextview.setText(newtimermsg);
    int newtimer = Integer.parseInt(newtimermsg);
    bomb1.setTimer(newtimer);
    bomb1.setState(true);
}

您可能会注意到,此代码改编自 Google 网站上的 NFC 示例,如有任何帮助,我们将不胜感激。

还包括 AndroidManifest.xml 的应用程序部分

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.Jhadwin.passthebomb.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>            
    </activity>
    <activity android:name="com.Jhadwin.passthebomb.newgame"/>
    <activity android:name="com.Jhadwin.passthebomb.About"/>
    <activity android:name="com.Jhadwin.passthebomb.Help"/>
</application>

【问题讨论】:

  • 请在 AndroidManifest.xml 中发布您使用的意图过滤器。
  • 刚刚编辑添加了 AndroidManifest.xml,我在代码中使用了 AAR 方法而不是意图过滤器,因为它正在加载应用程序,而意图过滤器没有,它正在发布到标签收到申请。

标签: android nfc ndef nfc-p2p android-applicationrecord


【解决方案1】:

如果您使用 Android 应用记录 (AAR) 并且未在应用的清单中指定 NDEF_DISCOVERED 意图过滤器,Android 将不会知道您的应用在启动时可以处理 NFC 意图。因此,它将打开清单中的第一个活动,该活动声明一个 MAIN 意图过滤器,类别为 LAUNCHER,而不传递收到的 NDEF 消息。所以在你的情况下,com.Jhadwin.passthebomb.MainActivity 将被使用。

为了让 Android 将 NFC Intent(包括收到的 NDEF 消息)传递给您的 newgame 活动,您需要添加适当的 Intent 过滤器:

<activity android:name="com.Jhadwin.passthebomb.newgame">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="application/vnd.com.jhadwin.passthebomb.newgame" />
    </intent-filter>
</activity>

请注意,Android 中的意图过滤器是区分大小写的。为了避免混合大小写类型的问题,Android 会自动将 MIME 类型和 NFC 论坛外部类型名称转换为 LOWER-CASE(通常此类类型名称不区分大小写)。因此,您必须将 MIME 类型指定为全部 小写 才能实现匹配。

除此之外还有一些建议:

  1. Android 包名(以及一般的 Java 包名)只能使用小写字母。类名(包括活动)应以大写字母开头。

  2. 您应该更喜欢 NFC 论坛外部类型,而不是创建自定义应用程序特定的 MIME 类型:

    NdefMessage msg = new NdefMessage(new NdefRecord[] {
        NdefRecord.createExternal(
            "jhadwin.com",          // your domain name
            "passthebomb.newgame",  // your type name
            message.getBytes()),    // payload
            NdefRecord.createApplicationRecord("com.jhadwin.passthebomb")
    });
    

    在这种情况下,您可以使用这样的意图过滤器:

    <activity android:name="com.jhadwin.passthebomb.NewGame">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="vnd.android.nfc" android:host="ext"
                  android:pathPrefix="/jhadwin.com:passthebomb.newgame" />
        </intent-filter>
    </activity>
    

【讨论】:

  • 这很有帮助,我不得不删除我以前的代码,因为它不起作用,结果证明是我的错,试图处理我注释掉的消息,现在它起作用了,很多很抱歉,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多