2011-08-23 137 views
0

嗨大家好,我開發了一個NFC應用程序,並將它安裝到我的NEXUS S手機上..但顯示標籤時,應用程序未加載?我可不可以嗎?我怎麼過來吧..創建我的NFC應用程序

我的問題是,當nexus發現一個標籤,我的應用程序沒有列在「選擇一個動作」 - 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> 

請儘快請我真的卡住回覆..

回答

0

這可能是可能的,你缺少的是要與有關活動相關聯的意圖過濾器。

添加下面的一段在AndroidManifest.xml XML你的主要發射類下

<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> 

另外請注意,你需要根據資源的nfc_tech_filter.xml文件/ XML指定高科技列表。

希望幫助!