title: NFC Tag caught by android's default Tag Viewer instead of app
body: <p>I've followed differents tutorials and read some fix related to that issue and still, nothing seems to work... Whenever I scan a NFC tag, it opens the default android app called <em>Tag Viewer</em> telling me "<strong>New tag collected</strong>" and with the body "<strong>Empty tag</strong>" and nothing happens within my app. I can't get any log printed...</p>
  
  <p>Here is my manifest:</p>
  
  <pre><code>    &lt;activity
          android:name=".activityv2.ActivityHome"
          android:label="Home"
          android:screenOrientation="portrait" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.nfc.action.NDEF_DISCOVERED"/&gt;
              &lt;action android:name="android.nfc.action.TECH_DISCOVERED"/&gt;
              &lt;category android:name="android.intent.category.DEFAULT"/&gt;
          &lt;/intent-filter&gt;
          &lt;meta-data
              android:name="android.nfc.action.TECH_DISCOVERED"
              android:resource="@xml/nfc_tag_filter" /&gt;
      &lt;/activity&gt;
  </code></pre>
  
  <p>Here is my Activity:</p>
  
  <pre><code>public class ActivityHome extends GenericActivity implements BackHandledFragment.BackHandlerInterface, OnFragmentListener {
  
      private final String TAG = "ActivityHome";
  
  
      public static final String MIME_TEXT_PLAIN = "text/plain";
  
  
      @Bind(R.id.drawer_layout)
      DrawerLayout mDrawerLayout;
      @Bind(R.id.actionToolbar)
      Toolbar toolbar;
      @Bind(R.id.left_drawer_item)
      LinearLayout mDrawerLinear;
      @Bind(R.id.left_drawer_child)
      ListView mDrawerListChild;
      @Bind(R.id.profil_pic)
      CircleImageView mProfilPic;
      private NfcAdapter mNfcAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_v2_home);
          ButterKnife.bind(this);
  
          setSupportActionBar(toolbar);
          getSupportActionBar().setTitle(getResources().getString(R.string.app_name));
  
          mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
          // enable ActionBar app icon to behave as action to toggle nav drawer
  
  
          handleIntent(getIntent());
  
      }
  
  
      private void handleIntent(Intent intent) {
          String action = intent.getAction();
          if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
  
              String type = intent.getType();
              if (MIME_TEXT_PLAIN.equals(type)) {
  
                  Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                  new NdefReaderTask().execute(tag);
  
              } else {
                  Log.d(TAG, "Wrong mime type: " + type);
              }
          } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
  
              // In case we would still use the Tech Discovered Intent
              Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
              String[] techList = tag.getTechList();
              String searchedTech = Ndef.class.getName();
  
              for (String tech : techList) {
                  if (searchedTech.equals(tech)) {
                      new NdefReaderTask().execute(tag);
                      break;
                  }
              }
          }
      }
  
      @Override
      protected void onNewIntent(Intent intent) {
          /**
           * This method gets called, when a new Intent gets associated with the current activity instance.
           * Instead of creating a new activity, onNewIntent will be called. For more information have a look
           * at the documentation.
           *
           * In our case this method gets called, when the user attaches a Tag to the device.
           */
          handleIntent(intent);
      }
  
  
      private class NdefReaderTask extends AsyncTask&lt;Tag, Void, String&gt; {
  
          @Override
          protected String doInBackground(Tag... params) {
              Tag tag = params[0];
  
              Ndef ndef = Ndef.get(tag);
              if (ndef == null) {
                  // NDEF is not supported by this Tag.
                  return null;
              }
  
              NdefMessage ndefMessage = ndef.getCachedNdefMessage();
  
              NdefRecord[] records = ndefMessage.getRecords();
              for (NdefRecord ndefRecord : records) {
                  if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN &amp;&amp; Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
                      try {
                          return readText(ndefRecord);
                      } catch (UnsupportedEncodingException e) {
                          Log.e(TAG, "Unsupported Encoding", e);
                      }
                  }
              }
  
              return null;
          }
  
          private String readText(NdefRecord record) throws UnsupportedEncodingException {
          /*
           * See NFC forum specification for "Text Record Type Definition" at 3.2.1
           *
           * http://www.nfc-forum.org/specs/
           *
           * bit_7 defines encoding
           * bit_6 reserved for future use, must be 0
           * bit_5..0 length of IANA language code
           */
  
              byte[] payload = record.getPayload();
  
              // Get the Text Encoding
              String textEncoding = ((payload[0] &amp; 128) == 0) ? "UTF-8" : "UTF-16";
  
              // Get the Language Code
              int languageCodeLength = payload[0] &amp; 0063;
  
              // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
              // e.g. "en"
  
              // Get the Text
              return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
          }
  
          @Override
          protected void onPostExecute(String result) {
              if (result != null) {
                  Log.e(TAG, result);
              }
          }
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
          if (mNfcAdapter != null)
              stopForegroundDispatch(this, mNfcAdapter);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          if (mNfcAdapter != null)
              setupForegroundDispatch(this, mNfcAdapter);
      }
  
      @Override
      protected void onDestroy() {
          getSupportFragmentManager().removeOnBackStackChangedListener(mOnBackStackChangedListener);
          super.onDestroy();
      }
  
      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);
  
          IntentFilter[] filters = new IntentFilter[1];
          String[][] techList = new String[][]{};
  
          // Notice that this is the same filter as in our manifest.
          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 (IntentFilter.MalformedMimeTypeException e) {
              throw new RuntimeException("Check your mime type.");
          }
  
          adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
      }
  
      public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
          adapter.disableForegroundDispatch(activity);
      }
  
  }
  </code></pre>
  
  <p>Here is the XML config to catch them all:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
      &lt;tech-list&gt;
          &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.IsoDep&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcB&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcV&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NdefFormatable&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.MifareUltralight&lt;/tech&gt;
      &lt;/tech-list&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>And the message I get when I go near a tag and the default tag viewer opens:</p>
  
  <pre><code>04-20 12:45:26.825 799-2511/? I/ActivityManager: START u0 {flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras)} from uid 1027 on display 0
  04-20 12:45:26.874 1952-24878/? D/NativeNfcTag: Starting background presence check
  04-20 12:45:26.913 1952-4642/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 16000 Hz, output 48000 Hz
  04-20 12:45:26.917 799-24452/? I/ActivityManager: START u0 {act=android.nfc.action.TECH_DISCOVERED cmp=com.google.android.tag/com.android.apps.tag.TagViewer (has extras)} from uid 1027 on display 0
  04-20 12:45:26.947 799-1287/? I/ActivityManager: Start proc 24893:com.google.android.tag/u0a18 for activity com.google.android.tag/com.android.apps.tag.TagViewer
  </code></pre>