2012-11-29 3 views
0

this example에서 몇 가지 변경 사항을 적용하여 NFC 태그 리더/라이터를 개발했습니다.Android NFC 활동 onResume onNewIntent

"유형 X"와 "유형 Y"카드가 있습니다. 유형 X 카드는 Ndef 읽기/쓰기가 가능하고, 하나는 Mifare Classic이며, 하나는 Mifare Ultralight입니다. 유형 Y 카드는 Ndef 형식이며, 그 중 하나는 보호되어 (버스 카드), 다른 하나는 보호되지 않습니다.

장치 (갤럭시 S3)에서 소리가 세 개 있는데, 그 중 하나는 "아름다운 소리"이며 "이 카드에서 손쉽게 읽고 쓸 수 있습니다."라고 말합니다. 다른 하나는 "이 카드는 힘들지만 좋은 정보가 있습니다."라고 말하는 "좋은 소리"입니다. 마지막 하나는 "못 생겼어"라고 말하면서 "네가 나에게 NFC 태그를 보여주고있는 것을보고 있지만 나는 상관 없다"고 말했다.

응용 프로그램이 화면에서 실행 중일 때 유형 X 카드로 음질이 아름답고 읽기/쓰기가 가능하며 유형 Y 카드로 음질이 좋아졌으며 기술 정보를 얻을 수 있습니다.

그러나 백그라운드에서 응용 프로그램을 실행하고 장치의 기본 메뉴에있을 때 유형 X 카드로 아름다운 사운드를 얻었으며 모든 것이 훌륭하게 작동하고 유형 Y 카드를 탭하면 추악한 사운드가납니다 앱이 화면에 나타나지 않으면 아무 일도 일어나지 않습니다. 당신이 내 코드의 일부를 볼 수 아래

, 나는 onResume 또는 onNewIntent 작업에 뭔가를하고 있어요 생각합니다. 코드에 따라 무엇이 잘못 될 수 있는지, 또는 이런 종류의 문제를 일으킬만한 다른 아이디어가 있습니까? 감사합니다 내 ONSTART :

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 

    setContentView(R.layout.main); 
    findViewById(R.id.write_tag).setOnClickListener(mTagWriter); 
    mNote = ((EditText) findViewById(R.id.note)); 
    mTechNotes = ((TextView) findViewById(R.id.tech_notes)); 
    mNote.addTextChangedListener(mTextWatcher); 

    // Handle all of our received NFC intents in this activity. 
    mNfcPendingIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

    // Intent filters for reading a note from a tag or exchanging over p2p. 
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
    try { 
     ndefDetected.addDataType("text/plain"); 
    } catch (MalformedMimeTypeException e) { } 
    mNdefExchangeFilters = new IntentFilter[] { ndefDetected, tagDetected }; 

    // Intent filters for writing to a tag 

    mWriteTagFilters = new IntentFilter[] { tagDetected }; 
} 

내 onResume :

protected void onResume() { 
    super.onResume(); 
    mResumed = true; 
    // Sticky notes received from Android 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 
     NdefMessage[] messages = getNdefMessages(getIntent()); 
     byte[] payload = messages[0].getRecords()[0].getPayload(); 
     setNoteBody(new String(payload)); 
     setTechNotesBody(getTagInfo(getIntent())) 
     setIntent(new Intent()); // Consume this intent. 
    } else if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) { 
     setTechNotesBody(getTagInfo(getIntent())); 
     setIntent(new Intent()); 
    } 
    enableNdefExchangeMode(); 
} 

내 onPause :

protected void onPause() { 
    super.onPause(); 
    mResumed = false; 
    mNfcAdapter.disableForegroundNdefPush(this); 
} 

내 onNewIntent가 :

protected void onNewIntent(Intent intent) { 
    // NDEF exchange mode 
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 
     NdefMessage[] msgs = getNdefMessages(intent); 
     promptForContent(msgs[0], getTagInfo(intent)); 
    } else if(!mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
     promptForContent(null, getTagInfo(intent)); 
    } 

    // Tag writing mode 
    if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
     Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     writeTag(getNoteAsNdef(), detectedTag); 
    } 
} 

답변

2

필터를 가지고 있지했다면 매니페스트에 응용 프로그램이 태그를 처리하기 위해 실행되지 않습니다.