简体   繁体   English

Android NFC活动onResume onNewIntent

[英]Android NFC Activity onResume onNewIntent

I developed a NFC Tag reader/writer by making some changes on this example . 我通过对此示例进行一些更改来开发NFC标签读取器/写入器。

I have "type X" and "type Y" cards. 我有“X型”和“Y型”牌。 Type X cards are Ndef read/writeable, one is Mifare Classic, one is Mifare Ultralight. X型卡是Ndef可读/可写的,一个是Mifare Classic,一个是Mifare Ultralight。 Type Y cards are Ndef Formatable, one of them is protected(bus card), and the other one is not. Y型卡是Ndef Formatable,其中一个是受保护的(总线卡),另一个不是。

I get three sounds from the device (Galaxy S3), one of them is the "beautiful-sound", saying "It's easy to write and read from this card, allright". 我从设备(Galaxy S3)得到三个声音,其中一个是“美妙的声音”,说“这张卡片很容易写入和读取,好吧”。 The other one is the "good-sound", saying "This card is tough, but I got some information for you.". 另一个是“好听的”,说“这张卡很难,但我收到了一些信息。” The last one is the "ugly-sound", saying "I see you're showing me an NFC Tag, but I don't care." 最后一个是“丑陋的声音”,说“我看到你给我看了一个NFC标签,但我不在乎。”

When I got the application on the screen and running, I got beautiful-sound with Type X cards, can read/write and I got good-sound with Type Y cards and I can at least get their tech info. 当我在屏幕上运行应用程序并运行时,我可以使用X型卡获得漂亮的声音,可以读/写,并且我使用Y型卡获得了良好的声音,我至少可以获得他们的技术信息。

However, When I got the application running on background and I'm at the main menu of the device, I got beautiful sound with Type X cards and everything works great and when I tap Type Y cards I get ugly-sound and the app doesn't show up on the screen, nothing happens. 然而,当我在背景上运行应用程序并且我在设备的主菜单时,我用X型卡获得了美妙的声音,一切都很好,当我点击Y型卡时,我听到丑陋的声音,应用程序没有没有出现在屏幕上。

Below you can see parts of my code, I think I'm doing something wrong with onResume or onNewIntent operations. 下面你可以看到我的代码的一部分,我认为我在onResumeonNewIntent操作上做错了。 Any idea what can be wrong according to the code, or is there anything else that might cause this kind of thing? 根据代码知道什么可能是错的,或者还有什么可能导致这种事情? Thanks My onStart: 谢谢我的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 };
}

My onResume: 我的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();
}

My onPause: 我的onPause:

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

My onNewIntent: 我的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);
    }
}

如果清单中没有过滤器,则不会启动应用程序来处理标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM