2013-08-01 101 views
0

onNewIntent(意圖意圖)方法沒有被調用。NFC - 無法檢測標記

我努力的代碼是

public class NfcTagDetector extends Activity{ 

private NfcAdapter mNfcAdapter; 
private PendingIntent pendingIntent; 
private IntentFilter[] intentFiltersArray; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.detect_tag); 
    pendingIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    IntentFilter discovery=new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);   
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 

    // Intent filters for writing to a tag 
    intentFiltersArray = new IntentFilter[] { discovery,ndefDetected,techDetected }; 
} 

public void onPause() { 
    super.onPause(); 
    mNfcAdapter.disableForegroundDispatch(this); 
} 

public void onResume() { 
    super.onResume(); 
    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    // TODO Auto-generated method stub 
    super.onNewIntent(intent); 
    Log.e("TAP", "onNewIntent"); 
} 

}

而且AndroidManifest.xml中

<activity android:name="com.example.detect.NfcTagDetector" > 
     <intent-filter> 
      <action android:name="android.nfc.action.TECH_DISCOVERED" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.nfc.action.TECH_DISCOVERED" 
      android:resource="@xml/nfc_tech_filter" /> 
    </activity> 

nfc_tech_filter.xml文件

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
<tech-list> 
    <tech>android.nfc.tech.IsoDep</tech> 
    <tech>android.nfc.tech.NfcA</tech> 
    <tech>android.nfc.tech.NfcB</tech> 
    <tech>android.nfc.tech.NfcF</tech> 
    <tech>android.nfc.tech.NfcV</tech> 
    <tech>android.nfc.tech.Ndef</tech> 
    <tech>android.nfc.tech.NdefFormatable</tech> 
    <tech>android.nfc.tech.MifareClassic</tech> 
    <tech>android.nfc.tech.MifareUltralight</tech> 
</tech-list> 

請幫忙...

+1

你試圖檢測什麼樣的標籤?我認爲這個問題可能在你的 Peanut

+0

@Peanut是的,你是對的,你知道什麼..問題是我的設備。我試圖在三星S II和它的NFC是有缺陷的..現在我試圖在Nexus和東西都在工作.. – aditya

回答

1

tech-list需要一個標籤來匹配列表中的所有項目。這是不可能與您的名單。更好的嘗試:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
<tech-list> 
    <tech>android.nfc.tech.NfcA</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcB</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcF</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NfcV</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.Ndef</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.NdefFormatable</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.MifareClassic</tech> 
</tech-list> 
<tech-list> 
    <tech>android.nfc.tech.MifareUltralight</tech> 
</tech-list> 
</resources> 
+0

是的,你是對的.. Nd最大的問題是,我正在一個有缺陷的NFC設備上測試代碼..感謝nyways NFC傢伙.. +1爲你的幫助.. – aditya