方法NdefRecord.createTextRecord("en","string")在API级别21以下无法正常工作 [英] Method NdefRecord.createTextRecord("en" , "string") not working below API level 21

查看:100
本文介绍了方法NdefRecord.createTextRecord("en","string")在API级别21以下无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在装有Android Lollipop(5.x)或棉花糖(6.0)的设备上使用此代码时,该代码可以正常工作:

This code works fine when I use it on a device with Android Lollipop (5.x) or Marshmallow (6.0):

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NdefMessage createNdfMessage(String content) {
    NdefRecord record = NdefRecord.createTextRecord("en", content);
    NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
    return msg;
}

但是,当我在装有Android 4.2.2(API级别17)的设备上尝试此操作时,我的应用程序崩溃了.我如何使用此代码在低于21(这是方法NdefRecord.createTextRecord可用的API级别)的API级别上创建文本记录?

But when I try this on a device with Android 4.2.2 (API level 17) my app crashes. How can I use this code to create a Text record on API levels below 21 (that's the API level where the method NdefRecord.createTextRecord became available)?

推荐答案

方法NdefRecord.createTextRecord()是在API级别21中引入的.因此,该方法在该API级别以下的平台上不可用.但是,您可以轻松地自行组装Text记录.文本记录的有效负载由状态字节,语言代码字段和文本字段组成:

The method NdefRecord.createTextRecord() was introduced in API level 21. Consequently, it is not available on platforms below that API level. However, you could easily assemble the Text record on your own. The payload of a Text record consists of a status byte, a language code field, and a text field:


+-------------+---------------+--------------------------+
| Status byte | Language code | Text                     |
| (1 byte)    | (n byte)      | (m byte)                 |
+-------------+---------------+--------------------------+

  • 状态字节在第7位指示文本字段的字符编码(0 = UTF-8,1 = UTF-16),在第5..0位指示语言代码的长度n. .位6必须始终为零.
  • 归档的语言代码包含以US-ASCII(例如"en")编码的IANA语言代码.
    • The status byte indicates the character encoding of the text field (0 = UTF-8, 1 = UTF-16) in bit 7 and the length n of the language code in bits 5..0. Bit 6 must always be zero.
    • The language code filed contains an IANA language code encoded in US-ASCII (e.g. "en").
    • 您可以使用以下方法创建文本记录:

      You could create the Text record using this method:

      public static NdefRecord createTextRecord(String language, String text) {
          byte[] languageBytes;
          byte[] textBytes;
          try {
              languageBytes = language.getBytes("US-ASCII");
              textBytes = text.getBytes("UTF-8");
          } catch (UnsupportedEncodingException e) {
              throw new AssertionError(e);
          }
      
          byte[] recordPayload = new byte[1 + (languageBytes.length & 0x03F) + textBytes.length];
      
          recordPayload[0] = (byte)(languageBytes.length & 0x03F);
          System.arraycopy(languageBytes, 0, recordPayload, 1, languageBytes.length & 0x03F);
          System.arraycopy(textBytes, 0, recordPayload, 1 + (languageBytes.length & 0x03F), textBytes.length);
      
          return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, null, recordPayload);
      }
      
      NdefRecord r = createTextRecord("en", content);
      

      这篇关于方法NdefRecord.createTextRecord("en","string")在API级别21以下无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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