简体   繁体   English

扫描NFC标签时启动特定活动

[英]Launch a specific activity when scanning NFC tag

I'm trying to launch a specific activity when my phone scans an NFC Tag. 我正在尝试在手机扫描NFC标签时启动特定活动。 This is what my manifest looks like: 这就是我的清单的样子:

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

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.lgandroid.ddcnfc.BluePrintActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:mimeType="application/com.lgandroid.ddcnfc"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    <activity
        android:name="com.lgandroid.ddcnfc.LoginActivity"
        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.lgandroid.ddcnfc.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.lgandroid.ddcnfc.PointDiagnosisActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.lgandroid.ddcnfc.PointControlActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.lgandroid.ddcnfc.SystemDiagnosisActivity"
        android:label="@string/app_name" >
    </activity>



    <activity android:name="com.lgandroid.ddcnfc.SettingsActivity" android:label="@string/app_name"></activity>
</application>

Whenever I scan my tag, my main activity launches but I'd like my BluePrintActivity to launch. 每当我扫描我的标签时,我的主要活动就会启动,但我希望我的BluePrintActivity能够启动。 I'm not sure why this is the case. 我不确定为什么会这样。 Here is my code for writing to tag: 这是我写入标签的代码:

private boolean writeTag(Tag tag) {
        NdefRecord appRecord = NdefRecord.createApplicationRecord("com.lgandroid.ddcnfc");
        NdefMessage message = new NdefMessage(new NdefRecord[] { appRecord });

        try {
            // see if tag is already NDEF formatted
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                ndef.connect();

                if (!ndef.isWritable()) {
                    nfcTextView.setText("Read-only tag.");
                    return false;
                }

                // work out how much space we need for the data
                int size = message.toByteArray().length;
                if (ndef.getMaxSize() < size) {
                    nfcTextView.setText("Tag doesn't have enough free space.");
                    return false;
                }

                ndef.writeNdefMessage(message);
                nfcTextView.setText("Tag written successfully.");
                return true;
            } else {
                // attempt to format tag
                NdefFormatable format = NdefFormatable.get(tag);
                if (format != null) {
                    try {
                        format.connect();
                        format.format(message);
                        nfcTextView.setText("Tag written successfully!\nClose this app and scan tag.");
                        return true;
                    } catch (IOException e) {
                        nfcTextView.setText("Unable to format tag to NDEF.");
                        return false;
                    }
                } else {
                    nfcTextView.setText("Tag doesn't appear to support NDEF format.");
                    return false;
                }
            }
        } catch (Exception e) {
            nfcTextView.setText("Failed to write tag");
        }

        return false;
    }

Edit: The answer I accepted above hinted me towards the right direction but since I was writing to a tag, the code in the accepted answer is not exactly the correct solution. 编辑:我上面接受的答案暗示我朝着正确的方向,但由于我正在写一个标签,接受的答案中的代码并不完全正确。 If you are writing to a tag, this is what you need to do: 如果您要写入标签,则需要执行以下操作:

 NdefRecord appRecord = new NdefRecord(
            NdefRecord.TNF_MIME_MEDIA ,
            "application/com.lgandroid.ddcnfc".getBytes(Charset.forName("US-ASCII")),
            new byte[0], new byte[0]);
    NdefMessage message = new NdefMessage(new NdefRecord[] { appRecord });

If you want to store a payload, just replace the last parameter "new byte[0]" to an appropriate data. 如果要存储有效负载,只需将最后一个参数“new byte [0]”替换为适当的数据。

The reason your app starts is because you write an Android Application Record to the tag. 您的应用启动的原因是您在标签上写了一个Android应用程序记录。 This causes the application that has a matching package name to start up instead of the filtered activity. 这会导致具有匹配包名称的应用程序启动而不是已过滤的活动。

Because you are filtering for a mime type you want to create a Mime Record with type 'application/com.lgandroid.ddcnfc' so instead of 因为您要为mime类型进行过滤,所以要创建类型为“application / com.lgandroid.ddcnfc”的Mime记录,而不是

NdefRecord appRecord = NdefRecord.createApplicationRecord("com.lgandroid.ddcnfc");

You should use: 你应该使用:

NdefRecord appRecord = NdefRecord.createMimeRecord("application/com.lgandroid.ddcnfc", byteArray);

As long as you only have an AAR stored on the tag, your application will be launched with its default activity (or the current activity stack, whatever it is). 只要您在标记上只存储了AAR,您的应用程序就会以其默认活动(或当前活动堆栈,无论它是什么)启动。 Therefore, the AAR should be the last record stored on a tag, only used to identify the app. 因此,AAR应该是存储在标签上的最后一条记录,仅用于识别应用程序。

If you have an additional NDEF record that is matched by one of your Activities, there is a chance that the according Activity will be opened to handle the tag. 如果您有一个与您的某个活动匹配的其他NDEF记录,则可能会打开相应的活动来处理该标记。 However, my experiments and another question here indicate that this mechanism does not work as advertised. 但是, 我的实验此处的另一个问题表明这种机制不像宣传的那样有效。

An alternative solution might be to store a URL as one of the messages on the tag, opening a web page on a server you control (or going directly to Google Play). 另一种解决方案可能是将URL存储为标记上的消息之一,在您控制的服务器上打开网页(或直接转到Google Play)。

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

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