Get priority to receive events when a contactless card is discovered at the NFC interface - Android Network

Android examples for Network:NFC

Description

Get priority to receive events when a contactless card is discovered at the NFC interface

Demo Code


//package com.java2s;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;

import android.nfc.tech.IsoDep;

public class Main {
    /**// w  w w  .  java  2s .co  m
     * Get priority to receive events when a contactless card is discovered at the NFC interface
     * @param activity the activity that will get the priority
     * @param adapter the local (phone's) NfcAdapter
     */
    public static void enableForegroundDispatch(Activity activity,
            NfcAdapter adapter) {
        Intent intent = activity.getIntent();
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (adapter.isEnabled()) {
            PendingIntent tagIntent = PendingIntent.getActivity(activity,
                    0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            IntentFilter iso = new IntentFilter(
                    NfcAdapter.ACTION_TECH_DISCOVERED);
            adapter.enableForegroundDispatch(
                    activity,
                    tagIntent,
                    new IntentFilter[] { iso },
                    new String[][] { new String[] { IsoDep.class.getName() } });
        }
    }
}

Related Tutorials