title: Writing plain text to an NFC tag, problems with intents
body: <p>I'm trying to make an application that writes a simple plain text message to an NFC tag. The user writes a message in the EditText box, then on button press, the app writes the text onto a tag.
  I've been following this tutorial: <a href="http://www.framentos.com/it/android-tutorial/2012/07/31/write-hello-world-into-a-nfc-tag-with-a/" rel="nofollow">http://www.framentos.com/it/android-tutorial/2012/07/31/write-hello-world-into-a-nfc-tag-with-a/</a>
  However, when i try to put the NFC tag up next to it, the phone opens up another app, instead of using mine that is already open. I know the problem's with the intents, but i don't want to declare the intent filter in the manifest, because i don't want the app to open up when the tag is placed next to the phone, i want the app to be already open beforehand.
  Here's the code for the application:</p>
  
  <pre><code>import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.nfc.FormatException;
  import android.nfc.NdefMessage;
  import android.nfc.NdefRecord;
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.Ndef;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  
  public class MainActivity extends AppCompatActivity {
  
      NfcAdapter adapter;
      PendingIntent pendingIntent;
      IntentFilter writeTagFilters[];
      Tag mytag;
      Context ctx;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ctx=this;
          Button btnWrite = (Button) findViewById(R.id.button);
          final EditText message = (EditText)findViewById(R.id.edit_message);
          btnWrite.setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View v) {
                  try {
                      if(mytag==null){
                          Toast.makeText(ctx, ctx.getString(R.string.error_detected), Toast.LENGTH_LONG).show();
                      }else{
                          write(message.getText().toString(),mytag);
                          Toast.makeText(ctx, ctx.getString(R.string.ok_writing), Toast.LENGTH_LONG ).show();
                      }
                  } catch (IOException e) {
                      Toast.makeText(ctx, ctx.getString(R.string.error_writing), Toast.LENGTH_LONG ).show();
                      e.printStackTrace();
                  } catch (FormatException e) {
                      Toast.makeText(ctx, ctx.getString(R.string.error_writing) , Toast.LENGTH_LONG ).show();
                      e.printStackTrace();
                  }
              }
          });
  
          adapter = NfcAdapter.getDefaultAdapter(this);
          pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
          IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
          tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
          writeTagFilters = new IntentFilter[] { tagDetected };
      }
  
      private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
  
          //create the message in according with the standard
          String lang = "en";
          byte[] textBytes = text.getBytes();
          byte[] langBytes = lang.getBytes("US-ASCII");
          int langLength = langBytes.length;
          int textLength = textBytes.length;
  
          byte[] payload = new byte[1 + langLength + textLength];
          payload[0] = (byte) langLength;
  
          // copy langbytes and textbytes into payload
          System.arraycopy(langBytes, 0, payload, 1, langLength);
          System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
  
          NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
          return recordNFC;
      }
  
      private void write(String text, Tag tag) throws IOException, FormatException {
  
          NdefRecord[] records = { createRecord(text) };
          NdefMessage message = new NdefMessage(records);
          Ndef ndef = Ndef.get(tag);
          ndef.connect();
          ndef.writeNdefMessage(message);
          ndef.close();
      }
  
      @Override
      protected void onNewIntent(Intent intent){
          if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
              mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
              Toast.makeText(this, this.getString(R.string.ok_detection) + mytag.toString(), Toast.LENGTH_LONG ).show();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
  
  
     return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help, i don't know what i'm doing wrong!
  I'm just a beginner at NFC and Android programming!</p>