My NFC app cannot launch after sensing the tag

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 2

Jul 9, 2012 4:53:53 AM via Website

Hi, everyone. I am a beginner of android application development. I am developing my first android app which will auto launch after sensing a NFC tag and read the data from the tag. I have read examples from the android development page and other sites like stackoverflow, so I discover I have to make use of the foreground-dispatch method to overcome the mission. Here are the codes I wrote, however, some problems exist cause it cannot launch after the mobile device senses the tag, could anyone please help me to take a look and give some advices to me. Thank you!

I have divided my program into two java files, here is the main activity file:
public class NFC extends Activity {

NFCForeGround nfcForeGround = null;
NdefMessage[] messages = null;

public void onCreate(Bundle savedInstanceState) {
//Create the activity
//Form the layout
//Change the background color of layout
View mlayout = findViewById(;
//Give value to the variable
nfcForeGround = new NFCForeGround(this);

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
return true;

public void onPause(){
//Pause the activity
//Disable NFC Foreground dispatch

public void onResume(){
//Resume the activity
//Enable the Foreground dispatch

public void onNewIntent(Intent intent){
//Set the intent
//Get the action
String action = intent.getAction();
//Check if the action equals to Discover a NFC tag
//Fetch the message
Parcelable[] rawMessage =
//Check if rawMessage is null
//Check the rawMessage into NdefMessage format
messages = new NdefMessage[rawMessage.length];
for(int i=0; i<rawMessage.length; i++)
messages[i] = (NdefMessage) rawMessage[i];
//Message is not in Parcelable format
byte[] empty = new byte[] {};
//Fetch message and switch the format
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
NdefMessage message = new NdefMessage(new NdefRecord[]{
messages = new NdefMessage[]{


And below is the class of the NFC object:
public class NFCForeGround {
private NfcAdapter nfcadapter;
//Variables needed for using the enableForegroundDispatch function
private PendingIntent intent;
private Activity act;
private IntentFilter FilterArray[];
private String TechLists[][];

public NFCForeGround(Activity activity){
//Set the variable act to activity
this.act = activity;
//Give value to variable nfcadapter
nfcadapter = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());

//Give value to variable intent
intent = PendingIntent.getActivity(act, 0, new Intent(act,
act.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

//Give value to variable ndef
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

try {
//Filter for the intent NfcAdapter.ACTION_NDEF_DISCOVERED with mime type "type/plain"
} catch (MalformedMimeTypeException e) {

//Give value to FilterArray
FilterArray = new IntentFilter[]{ndef};
//Give value to TechLists
TechLists = new String[][]{new String[]{NfcA.class.getName()}};


public void enable(){
//Enable the Dispatch
nfcadapter.enableForegroundDispatch(act, intent, FilterArray, TechLists);

public void disable(){
//Disable the Dispatch

public NfcAdapter getNfcAdapter() {
//Return the NFC Adapter variable
return nfcadapter;

And below is the AndroidManifest file:
<manifest xmlns:android=""
android:versionName="1.0" >

android:targetSdkVersion="15" />
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<uses-permission android:name="android.permission.NFC"/>
android:theme="@style/AppTheme" >
android:label="@string/title_activity_nfc" >
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="mime/type"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />


At last is the nfc_tech_filter file:
<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

As you can see, I have added an intent-filter into the manifest file, also add a test list file to handle the tag, but I am not sure why it cannot work.
  • Forum posts: 112

Feb 4, 2013 1:13:22 PM via Website

which card you are using for testing?