0

我通过对此示例进行一些更改来开发 NFC 标签读取器/写入器。

我有“X 型”和“Y 型”卡。X 型卡是 Ndef 可读写的,一张是 Mifare Classic,一张是 Mifare Ultralight。Y 型卡是 Ndef Formatable 的,其中一张是受保护的(总线卡),另一张不是。

我从设备(Galaxy S3)中听到三种声音,其中一种是“美妙的声音”,说“这张卡很容易读写,好吧”。另一个是“好声音”,说“这张卡很硬,但我得到了一些信息给你。”。最后一个是“丑陋的声音”,说“我看到你在给我看一个 NFC 标签,但我不在乎。”

当我在屏幕上运行应用程序时,我使用 X 型卡获得了美妙的声音,可以读/写,并且使用 Y 型卡获得了良好的声音,我至少可以获得他们的技术信息。

但是,当我让应用程序在后台运行并且我在设备的主菜单中时,我使用 X 型卡获得了美妙的声音,并且一切正常,当我点击 Y 型卡时,我得到了难看的声音并且应用程序没有没有出现在屏幕上,什么也没有发生。

下面你可以看到我的部分代码,我认为我在onResumeonNewIntent操作上做错了。任何想法根据代码可能有什么问题,或者还有其他可能导致这种事情的东西吗?谢谢我的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 };
}

我的简历:

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();
}

我的暂停:

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);
    }
}
4

1 回答 1

2

If you've not got a filter in the manifest then your application won't be launched to handle the tag.

于 2012-11-29T21:27:26.060 回答