Example usage for android.nfc NfcAdapter enableForegroundDispatch

List of usage examples for android.nfc NfcAdapter enableForegroundDispatch

Introduction

In this page you can find the example usage for android.nfc NfcAdapter enableForegroundDispatch.

Prototype

public void enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters,
        String[][] techLists) 

Source Link

Document

Enable foreground dispatch to the given Activity.

Usage

From source file:com.villetainio.travelcardreminder.activities.MainActivity.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), ReadCardActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);/*from www. j av  a 2 s.c  o  m*/

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][] {};

    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType("*/*"); //TODO Use only needed MIME based dispatches.
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("MIME type not supported.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

From source file:com.example.mynsocial.BluetoothChat.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    Log.i(TAG, "---------------------------------------");
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);/* w ww  .  jav  a  2 s  . co m*/
    String[][] techList = new String[][] {};
    adapter.enableForegroundDispatch(activity, pendingIntent, null, techList);
}

From source file:com.dimasdanz.kendalipintu.NFCOpenDoorActivity.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);/* w ww .j ava  2s.co m*/

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][] {};
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }
    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

From source file:de.uni_koblenz_landau.apow.PatientListActivity.java

/**
 * Start scanning for NFC tags./*w ww .j  a  va2  s .c o m*/
  * @param activity The Activity requesting to the foreground dispatch.
  * @param adapter The NfcAdapter used for the foreground dispatch.
 */
private static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    if (adapter != null) {
        // Add intents for NFC.
        final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0,
                intent, 0);

        IntentFilter[] filters = new IntentFilter[1];
        String[][] techList = new String[][] {};

        filters[0] = new IntentFilter();
        filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
        filters[0].addCategory(Intent.CATEGORY_DEFAULT);

        adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    }
}

From source file:foundme.uniroma2.it.professore.HomeActivity.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);//from   ww w.  ja v a  2 s. co  m

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][] {};

    //Stesso filtro del Manifest.
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(Variables_it.MIME_TEXT_PLAIN);
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException(Variables_it.MIME_ERROR);
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

From source file:io.atrac613.AbstractNfcTagFragment.java

@Override
public void onResume() {
    Log.d(TAG, "*** AbstractNfcFeliCaTagFragment go Resume");

    //foregrandDispathch
    Activity a = this.getActivity();
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    try {/*from  www .j  a  v  a 2 s . c om*/
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    IntentFilter[] filters = new IntentFilter[] { ndef, tag, tech };

    PendingIntent pendingIntent = PendingIntent.getActivity(a, 0,
            new Intent(a, a.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this.getActivity());

    adapter.enableForegroundDispatch(this.getActivity(), pendingIntent, filters, registerTechList(mTechList));

    super.onResume();
}

From source file:de.uni_koblenz_landau.apow.LoginActivity.java

/**
 * Start scanning for NFC tags.//from w  w w .  ja  v a 2  s. co m
 * @param activity The Activity requesting to the foreground dispatch.
 * @param adapter The NfcAdapter used for the foreground dispatch.
 */
private static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    if (adapter != null) {
        // Add intents for NFC.
        final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0,
                intent, 0);

        IntentFilter[] filters = new IntentFilter[1];
        String[][] techList = new String[][] {};

        filters[0] = new IntentFilter();
        filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filters[0].addCategory(Intent.CATEGORY_DEFAULT);
        try {
            filters[0].addDataType(MIME_TEXT_PLAIN);
        } catch (MalformedMimeTypeException e) {
            e.printStackTrace();
        }

        adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    }
}

From source file:com.tananaev.passportreader.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    Intent intent = new Intent(getApplicationContext(), this.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    String[][] filter = new String[][] { new String[] { "android.nfc.tech.IsoDep" } };
    adapter.enableForegroundDispatch(this, pendingIntent, null, filter);
}

From source file:it.imwatch.nfclottery.MainActivity.java

/**
 * Sets up the dispatching of NFC events a the foreground Activity.
 *
 * @param activity The {@link android.app.Activity} to setup the foreground dispatch for
 * @param adapter  The {@link android.nfc.NfcAdapter} to setup the foreground dispatch for
 *///from  w w w.  j  a v  a 2  s. c o  m
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    if (adapter != null) {
        final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0,
                intent, 0);
        IntentFilter[] filters = new IntentFilter[1];
        String[][] techList = new String[][] {};

        // Notice that this is the same filter we define in our manifest.
        // This ensures we're not stealing events for other types of NDEF.
        filters[0] = new IntentFilter();
        IntentFilter filter = filters[0];
        filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        try {
            filter.addDataType(MIME_VCARD);
            filter.addDataType(MIME_XVCARD);
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("Mime type not supported.");
        }

        adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    }
}

From source file:com.geecko.QuickLyric.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    App.activityResumed();/* ww  w  . j a v  a2 s.c o  m*/
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ((Object) this).getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("application/lyrics");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            return;
        }
        IntentFilter[] intentFiltersArray = new IntentFilter[] { ndef, };
        try {
            nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);
        } catch (Exception ignored) {
        }
    }
    LyricsViewFragment lyricsViewFragment = (LyricsViewFragment) getFragmentManager()
            .findFragmentByTag(LYRICS_FRAGMENT_TAG);
    if (lyricsViewFragment != null) {
        if (getIntent() == null || getIntent().getAction() == null || getIntent().getAction().equals("")) {
            // fixme executes twice?
            if (!"Storage".equals(lyricsViewFragment.getSource()) && !lyricsViewFragment.searchResultLock)
                lyricsViewFragment.fetchCurrentLyrics(false);
            lyricsViewFragment.checkPreferencesChanges();
        }
    }
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(id.appbar);
    appBarLayout.removeOnOffsetChangedListener(this);
    appBarLayout.addOnOffsetChangedListener(this);
}