2011-09-06 140 views
0

我正在寫一個Android應用程序來使用Nexus S讀取和寫入標記。 我有兩個不同的活動,一個用於閱讀,另一個用於寫作,兩者都在各自的環境中調用。我的清單看起來是這樣的:NFC讀取和寫入

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="bri.nfc.NfcTagReaderV1" 
     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"> 
     <activity android:name=".NFCTagReaderV1Activity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="com.example.android.nfc.TagViewer"> 
      <intent-filter> 
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
      <data android:mimeType="text/*" /> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
      <data android:scheme="http" /> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 
    <activity android:name="bri.nfc.writetag.WritetagActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
      <data android:mimeType="text/*" /> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
      <data android:scheme="http" /> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 
</application> 
</manifest> 

在我的執行,該活動被稱爲隨機..一個時間tagviewer活動被稱爲另一次writetag活動被調用。

我需要我的應用程序在讀取時調用「tagviewer活動」,並在寫入時調用「寫入活動」。

回答

0

你不需要爲你的「書寫活動」聲明一個意圖過濾器。相反,您需要使用前臺調度系統。

以下鏈接會將您發送到Android Dev頁面,該頁面介紹如何實施前臺調度系統。實施非常簡單。

http://developer.android.com/guide/topics/nfc/index.html#foreground-dispatch

當newIntent事件被觸發,那麼你可以寫在NFC標籤上的數據。對於在NFC標籤上寫入數據,請點擊此鏈接http://developer.android.com/guide/topics/nfc/index.html#write

這就是我在應用程序中執行此操作的方式,它的工作原理很棒。

0

當您的活動恢復時,您應該在ActivityClass的OnResume Method()中使用前臺調度。對於閱讀標籤,請點擊此鏈接Reading NFC Tag With ForeGround Dispatch。並且在編寫時,您應該再次設置WritingActivity的前臺調度。不要忘記在你的Activity的OnPause方法中停止Foreground Dispatch。因此,一次只有1個活動將註冊爲前臺調度