繁体   English   中英

如何在不同的活动中读写NFC卡

[英]How to read/write NFC card in different activities

我有两个活动。 我想使用第一个活动(如MainAcitivity )来读取卡,然后使用第二个活动来写入卡。 因为发现卡后必须激活该活动。 因此,我将以下设置用于这两项活动:

        </intent-filter>
        <!-- Handle notes detected from outside our application -->
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>

但是,我的问题是,当我参加第二个活动并扫描NFC卡时,电话将显示第一个活动和第二个活动的意图选择器。

因此,当我处于第二个活动(以及相反)中时,如何通过代码禁用第一个活动的NDEF_DISCOVERED意图过滤器?

这是完整的AndroidManifest文件:

   <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize|screenLayout"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- Handle notes detected from outside our application -->
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".SecondActivity"
            android:label="@string/app_name">
           <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>

为了强制当前前台中的活动接收NFC发现事件,您可以使用前台调度系统阅读器模式API

如果当前处于前台,则这两种方法都将在接收NFC发现事件时给予活动优先级。

因此,您可以使用例如:

public void onResume() {
    super.onResume();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // to catch all NFC discovery events:
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);

    // or to only catch text/plain NDEF records (as you currently do in your manifest):
    //IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    //try {
    //    ndef.addDataType("text/plain");
    //} catch (MalformedMimeTypeException e) {}
    //nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] { ndef }, null);
}

public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}

public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) ||
        NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // handle NFC events ...
    }
}

此外,如果您不希望通过NFC发现事件启动活动,则无需在该活动的mainfest中声明android.nfc.action.*_DISCOVERED意向过滤器。

暂无
暂无

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

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