Example usage for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED

List of usage examples for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED

Introduction

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

Prototype

String ACTION_NDEF_DISCOVERED

To view the source code for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED.

Click Source Link

Document

Intent to start an activity when a tag with NDEF payload is discovered.

Usage

From source file:net.lp.hivawareness.v4.HIVAwarenessActivity.java

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

    if (!HIVAwarenessActivity.DEBUG)
        FlurryAgent.onEvent("window_displayed");
    if (!HIVAwarenessActivity.DEBUG)
        FlurryAgent.onPageView();//from   ww w  .  ja  v a 2 s .  c  o m
    AnalyticsUtils.getInstance().trackGAPageView("/" + this.getLocalClassName(), "");

    // Check to see that the Activity started due to an Android Beam
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
    }

    // TODO sending messages at the same time do not work !race condition
    if (!(DEBUG && mNfcAdapter == null)) {
        mNfcAdapter.enableForegroundNdefPush(this, createNdefMessage());
        mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFiltersArray, mTechListsArray);
    }

    NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(1);

    if (caught != -1) {
        goToStartedFragment();
    }
}

From source file:edu.cmu.mpcs.dashboard.TagViewer.java

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);/* w w w . jav  a2  s  .  c  o  m*/
    Log.d("TAG_VIEWER", "on new intent actually fired ");
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

        resolveIntent(intent);
    }

    // Tag writing mode
    if (writeContent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Log.d("TAG_VIEWER", "in Oncreate Before writing");

        // new AlertDialog.Builder(TagViewer.this)
        // .setTitle("Touch tag to write")
        // .setOnCancelListener(
        // new DialogInterface.OnCancelListener() {
        // public void onCancel(DialogInterface dialog) {
        // disableTagWriteMode();
        //
        // }
        // }).create().show();

        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        try {
            WriteToTag.write(detectedTag, writeContent);
            Context context = getApplicationContext();
            CharSequence text = "Tag sucessfully written!!";
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            Log.d("TAG_VIEWER", "Before calling dialog.cancel");
            dialog.dismiss();
            finish();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d("TAG_VIEWER", "in on create, writing to tag");
    }
    //
    // // Tag writing mode
    // if (mWriteMode
    // &&
    // NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
    // {
    // Tag detectedTag =
    // intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    // try {
    // WriteToTag.write(detectedTag);
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (FormatException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // Log.d("TAG_VIEWER", "in on new intent, writing to tag");
    // }
}

From source file:edu.cmu.mpcs.dashboard.TagViewer.java

private void enableTagWriteMode() {
    Log.d("TAG_VIEWER", "in enable tag write");
    mWriteMode = true;//from   ww w .  j  av  a  2s . c  o  m
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    // tagDetected.addDataScheme("http");
    // IntentFilter techFilter = new
    // IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    try {
        tagDetected.addDataType("text/plain");
        // tagDetected.addDataScheme("http");
        // techFilter.addDataType("text/plain");
    } catch (MalformedMimeTypeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    IntentFilter httpDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    httpDetected.addDataScheme("http");

    mWriteTagFilters = new IntentFilter[] { tagDetected, httpDetected };
    mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mWriteTagFilters, null);
}

From source file:org.mozilla.gecko.BrowserApp.java

@Override
protected void onNewIntent(Intent intent) {
    String action = intent.getAction();

    final boolean isViewAction = Intent.ACTION_VIEW.equals(action);
    final boolean isBookmarkAction = GeckoApp.ACTION_HOMESCREEN_SHORTCUT.equals(action);
    final boolean isTabQueueAction = TabQueueHelper.LOAD_URLS_ACTION.equals(action);

    if (mInitialized && (isViewAction || isBookmarkAction)) {
        // Dismiss editing mode if the user is loading a URL from an external app.
        mBrowserToolbar.cancelEdit();/*  w  w  w  .j a v  a2s .  c o  m*/

        // Hide firstrun-pane if the user is loading a URL from an external app.
        hideFirstrunPager();

        if (isBookmarkAction) {
            // GeckoApp.ACTION_HOMESCREEN_SHORTCUT means we're opening a bookmark that
            // was added to Android's homescreen.
            Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.HOMESCREEN);
        }
    }

    showTabQueuePromptIfApplicable(intent);

    super.onNewIntent(intent);

    if (AppConstants.MOZ_ANDROID_BEAM && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        String uri = intent.getDataString();
        GeckoAppShell.sendEventToGecko(GeckoEvent.createURILoadEvent(uri));
    }

    // Only solicit feedback when the app has been launched from the icon shortcut.
    if (GuestSession.NOTIFICATION_INTENT.equals(action)) {
        GuestSession.handleIntent(this, intent);
    }

    // If the user has clicked the tab queue notification then load the tabs.
    if (AppConstants.NIGHTLY_BUILD && AppConstants.MOZ_ANDROID_TAB_QUEUE && mInitialized && isTabQueueAction) {
        Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.NOTIFICATION,
                "tabqueue");
        ThreadUtils.postToBackgroundThread(new Runnable() {
            @Override
            public void run() {
                openQueuedTabs();
            }
        });
    }

    if (!mInitialized || !Intent.ACTION_MAIN.equals(action)) {
        return;
    }

    // Check to see how many times the app has been launched.
    final String keyName = getPackageName() + ".feedback_launch_count";
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();

    // Faster on main thread with an async apply().
    try {
        SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
        int launchCount = settings.getInt(keyName, 0);
        if (launchCount < FEEDBACK_LAUNCH_COUNT) {
            // Increment the launch count and store the new value.
            launchCount++;
            settings.edit().putInt(keyName, launchCount).apply();

            // If we've reached our magic number, show the feedback page.
            if (launchCount == FEEDBACK_LAUNCH_COUNT) {
                GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Feedback:Show", null));
            }
        }
    } finally {
        StrictMode.setThreadPolicy(savedPolicy);
    }
}