简体   繁体   English

Android Application Record与pre-ICS的兼容性

[英]Android Application Record compatibility with pre-ICS

There's a new method in NdefRecord that allows writing AndroidApplicationRecord to the NdefMessage. NdefRecord中有一个新方法,可以将AndroidApplicationRecord写入NdefMessage。 This was not necessary in pre Ice-Cream-Sandwich, but since then if you want to handle specific URI from a NFC tag in your application (like defined in the intent-filter) it will not be delivered to your application, unless you define that record. 在Ice-Cream-Sandwich之前的版本中这不是必需的,但是从那时起,如果您想处理应用程序中NFC标签中的特定URI(如intent过滤器中定义的),则除非您定义了它,否则它不会传递给您的应用程序该记录。

createApplicationRecord(String packageName);

This is not available with some kind of compatibility package (I didn't find one), but the implementaion is fairly simple. 这不适用于某种兼容性软件包(我没有找到),但是实现非常简单。

First add your NdefRecord you want to be readable by any NFC device (remember that URI can be formatted/shortened with URI_PREFIX_MAP ) 首先添加您希望被任何NFC设备读取的NdefRecord(请记住URI可以使用URI_PREFIX_MAP进行格式化/缩短)

NdefRecord[] nr = new NdefRecord[2];
nr[0] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], uriBytes);

Add your AAR in the next place 在下一个位置添加您的AAR

static final byte[] RTD_ANDROID_APP = "android.com:pkg".getBytes();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    nr[1] = NdefRecord.createApplicationRecord("your.package.name");
else
    nr[1] = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, RTD_ANDROID_APP, new byte[] {}, "your.package.name".getBytes());

You do not need an AAR to handle a specific URI. 您不需要AAR即可处理特定的URI。 The AAR is just another method to guarantee that your app is started instead of another app that can handle the same URI. AAR只是保证您的应用程序已启动的另一种方法,而不是另一个可以处理相同URI的应用程序。

The AAR guarantees on ICS that your app receives the NDEF message. AAR在ICS上保证您的应用程序收到NDEF消息。 It also does not have to be the first record in the NDEF message (which is what the Intent filter will be matched against). 它也不必是NDEF消息中的第一条记录(这是与Intent过滤器匹配的内容)。 So it is quite different from URI or MIME type matching in an Intent filter. 因此,它与Intent过滤器中的URI或MIME类型匹配完全不同。 However, an AAR uses an External Type for the NDEF record, which is a kind of record that is not supported pre-ICS. 但是,AAR对NDEF记录使用外部类型,这是ICS之前不支持的一种记录。 So normally you should not use it as the first record of your NDEF message if you want it to work with pre-ICS devices. 因此,如果您希望它与ICS之前的设备一起使用,通常不应将其用作NDEF消息的第一条记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM