【问题标题】:Able to resolve NFC text records but not uri?能够解析 NFC 文本记录但不能解析 uri?
【发布时间】:2012-08-28 06:37:05
【问题描述】:

好的,所以我一直在使用我的第一个 Android 应用程序,NFC 非常受欢迎。我能够成功拾取纯文本类型的记录,但是当我切换到尝试拾取 uri 记录时,手机的浏览器会继续打开而不是我的应用程序。在这一点上我一无所知,所以这就是我得到的......

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="http" android:host="google.com"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:mimeType="text/plain"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

当我阅读标签时,我得到了一个新的意图,但它的动作类型是“MAIN”。只是重新启动吗?如果是这样,为什么文本记录不做同样的事情?我尝试了多个 uri 记录,每次我得到这种行为。这是java src的一部分。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent launchIntent = getIntent();
    String action = launchIntent.getAction();
    if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        Log.i(tag, "MATCH!");
    }


    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

    tagDetected.addDataScheme("http");
    tagDetected.addDataAuthority("google.com", null);
    filters = new IntentFilter[] { tagDetected };

    techArray = new String[][] {
            new String[] {
                    IsoDep.class.getName()
            },
            new String[] {
                    NfcB.class.getName()
            },
            new String[] {
                    Ndef.class.getName()
            }
    };

}

public void onResume(){
    super.onResume();



    nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, filters, techArray);

    Intent launchIntent = getIntent();
    String action = launchIntent.getAction();
    if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        Log.i(tag, "MATCH!");
    } else if(action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
        Log.i(tag, "TECH DISCOVERED");

    } else if(action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Log.i(tag, "TAG DISCOVERED");
    }

    Parcelable[] msg = launchIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    //byte[] payloadData = msg.getRecords()[0].getPayload();
    //Log.i(tag, "NUM records = " + Integer.toString(msg.getRecords().length));


}


public void onPause() {
    super.onPause();

    nfcAdapter.disableForegroundDispatch(this);
}

另一个有趣的注意事项是,当我在 enableForegroundDispatch() 调用中不包含技术列表时,应用程序根本不会接收任何由 NFC 产生的意图(在尝试读取 uri 记录时)。任何想法哦明智的互联网?!

【问题讨论】:

  • 标签上的URI是什么?主机名应与意图过滤器中的主机名完全匹配。
  • URI 由 0x01 组成,用于编码 http://www.,后跟 google.com。我认为这个问题与here讨论的问题有关。

标签: android android-intent uri nfc


【解决方案1】:

启用 ForegroundDispatch 后,intent 将通过onNewIntent() 到达,因此您需要在 Activity 中重写该方法以接收 NFC Intent。

您还需要确保主机名完全匹配。如果标签包含“www.google.com”,您的意图过滤器应包含相同的名称:&lt;data android:scheme="http" android:host="www.google.com"/&gt;tagDetected.addDataAuthority("www.google.com", null)

【讨论】:

  • 特别是在onNewIntent(Intent intent) 中添加this.setIntent(intent) 以了解它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
相关资源
最近更新 更多