创建我的NFC APP

时间:2011-08-23 10:03:49

标签: android tags nfc

  

大家好,我开发了一款NFC应用程序并将其安装到我的NEXUS S手机上......但是在显示标签时应用程序没有加载?我可以吗?我该如何过来......

     

我的问题是,当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>
  

请尽快回复,请真的卡住..

1 个答案:

答案 0 :(得分:0)

您可能缺少与相关活动相关联的intent过滤器。

在主启动器类

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

另请注意,您需要res / xml下的nfc_tech_filter.xml文件来指定技术列表。

希望有所帮助!!