android - 扫描 NFC 标签时启动特定 Activity

标签 android nfc

我正尝试在我的手机扫描 NFC 标签时启动特定 Activity 。这是我的 list 的样子:

<?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>

每当我扫描我的标签时,我的主要 Activity 都会启动,但我希望启动我的 BluePrintActivity。我不确定为什么会这样。这是我写入标签的代码:

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;
    }

编辑:我在上面接受的答案暗示我朝着正确的方向前进,但由于我正在写一个标签,接受的答案中的代码并不完全是正确的解决方案。如果你正在写一个标签,这就是你需要做的:

 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 });

如果要存储有效负载,只需将最后一个参数“new byte[0]”替换为合适的数据即可。

最佳答案

您的应用启动的原因是因为您向标签写入了 Android 应用程序记录。这会导致启动具有匹配包名称的应用程序而不是过滤的 Activity 。

因为您正在过滤 mime 类型,所以您希望创建类型为“application/com.lgandroid.ddcnfc”的 Mime 记录,而不是

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

你应该使用:

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

关于android - 扫描 NFC 标签时启动特定 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776307/

相关文章:

android - Google Play Android Developer API 更改同一次购买的初始购买时间

java - Android - 停止 Little Fluffy 位置服务

android - 使用嵌套选择 Android SQLite 的复杂更新查询

android - 应用程序中的旁遮普字体有问题吗?

java - 参数中的 NdefRecord.createMime 错误

android - nfc阅读器(acr122)如何检测到我的手机?

android - 在我的 N1 启动后,我的 BroadcastReceiver 没有收到 BOOT_COMPLETED Intent

android - 如何从我自己的应用程序打开 NFC

android - 通过 HCE 模拟信用卡

java - Google NFC API 和 Open NFC API 之间的区别