Creation of my NFC APP
Hi Everyone, i developed an NFC app and installed it over to my NEXUS S phone.. but on showing the tag the app didn't load?? may i no y?? and how shall i over come it..
my problem is that when the nexus discoveres a tag, my app is not listed in the "select an action"-popup.
public class TagReader extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = this.getIntent();
LinearLayout content = (LinearLayout) findViewById(R.id.list);
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
msgs[0] = (NdefMessage) rawMsgs[0];
}
NdefRecord[] record = msgs[0].getRecords();
Preconditions.checkArgument(record[0].getTnf() == NdefRecord.TNF_WELL_KNOWN);
Preconditions.checkArgument(Arrays.equals(record[0].getType(), NdefRecord.RTD_TEXT));
byte[] bRecord = record[0].getPayload();
String textEncoding = ((bRecord[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = bRecord[0] & 0077;
LayoutInflater inflater = LayoutInflater.from(this);
content.removeAllViews();
try{
String languageCode = new String(bRecord, 1, languageCodeLength, "US-ASCII");
String text = new String(bRecord, languageCodeLength + 1,bRecord.length - languageCodeLength - 1, textEncoding);
TextView textv = (TextView) inflater.inflate(R.layout.tag_text, content, false);
textv.setText(text);
content.addView(textv);
String x = null;
}catch(Exception e){
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nfc.TagReader"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.NFC"/>
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".TagReader"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Kindly reply as soon as possible please am really stu开发者_JS百科ck..
It could be possible that you are missing the intent filters that are to be associated with the activity concerned.
Add the following piece of xml in AndroidManifest.xml under your main launcher class
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
Also note that you would need the nfc_tech_filter.xml file under res/xml that specifies the tech-list.
Hope that helps !!
精彩评论