【问题标题】:NFC and MIME TYPE case sensitiveNFC 和 MIME TYPE 区分大小写
【发布时间】:2014-03-23 04:44:46
【问题描述】:
我只尝试基本版本的 NFC,但后来我发现 MIME TYPE 区分大小写。我的应用程序包名称有一个大写字母。
包名:com.example.Main_Activity
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/com.example.Main_Activity"/>
</intent-filter>
有办法解决吗?
【问题讨论】:
标签:
android
nfc
mime-types
intentfilter
ndef
【解决方案1】:
根据 RFC,MIME 类型不区分大小写。但是,Android 的意图过滤器匹配是区分大小写的。为了克服这个问题,您应该始终只使用小写 MIME 类型。
特别是使用 Android NFC API 的 MIME 类型记录辅助方法,MIME 类型将自动转换为仅小写字母。因此,使用大小写混合的类型名称调用方法 NdefRecord.createMime() 将始终导致创建仅小写的 MIME 类型名称。例如
NdefRecord r1 = NdefRecord.createMime("text/ThisIsMyMIMEType", ...);
NdefRecord r2 = NdefRecord.createMime("text/tHISiSmYmimetYPE", ...);
NdefRecord r3 = NdefRecord.createMime("text/THISISMYMIMETYPE", ...);
NdefRecord r4 = NdefRecord.createMime("text/thisismymimetype", ...);
将导致创建相同的 MIME 类型记录类型:
+----------------------------------------------------------+
| MIME:text/thisismymimetype | ... |
+----------------------------------------------------------+
所以你的意图过滤器也需要全小写:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/thisismymimetype" />
</intent-filter>
【解决方案2】:
我终于明白了。即使我的包名中包含大写字母,您也必须在代码和意图过滤器中以小写字母编写 MIME TYPE。
我知道实际上它们不是同一个包。但是,NFC 中的 MIME TYPE 仍会识别您的应用。只需确保在创建应用程序记录时编写正确的包。如果您注意到我必须使用包含 CAPS 的正确包名称。否则将找不到您的应用程序。
public NdefMessage createNdefMessage(NfcEvent event) {
String text = ("Beam me up, Android!\n\n" +
"Beam Time: " + System.currentTimeMillis());
NdefMessage msg = new NdefMessage(
new NdefRecord[] { NdefRecord.createMime(
"application/com.example.main_activity", text.getBytes())
/**
* The Android Application Record (AAR) is commented out. When
* a device receives a push with an AAR in it, the application
* specified in the AAR is guaranteed to run.
*
* The AAR overrides the tag dispatch system. You can add it
* back in to guarantee that this activity starts when
* receiving a beamed message. For now, this code uses
* the tag dispatch system.
*/
,NdefRecord.createApplicationRecord("com.example.Main_Activity")
});
return msg;
}
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/com.example.main_activity"/>
</intent-filter>