Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

android - NFC. Start a activity when scanning NDEF message

I'm trying to start a activity when my smartphone scans an NDEF message. This is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidbeam"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.NFC" />

    <uses-feature android:name="android.hardware.nfc" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidbeam.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.example.androidbeam.SendTextActivity" >

        </activity>
        <activity android:name="com.example.androidbeam.ReceiverNDEFActivity" >
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="ext"
                    android:pathPrefix="/com.example:externalType"
                    android:scheme="vnd.android.nfc" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Whenever I scan my NDEF message, my main activity launches but I'd like my ReceiverNDEFActivity to launch. I'm not sure why this is the case.

This is my NdefMessage:

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    byte[] payload= new String("Hello");
    String domain = "com.example";
    String type = "externalType"; 
    NdefRecord extRecord = NdefRecord.createExternal(domain, type, payload);

    NdefMessage msg = new NdefMessage(new NdefRecord[] { extRecord });
    return msg;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The trick is that NFC Forum external type names are case-insensitive. However, Android's intent filter system is case-SENSITIVE. Therefore, you must always use ALL lower-case external type names in your intent filters:

<activity android:name="com.example.androidbeam.ReceiverNDEFActivity" >
    <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="/com.example:externaltype" />
    </intent-filter>
</activity>

Note that the NdefRecord.createExternal(...) method will automatically convert all type names to lower-case spelling.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...