簡體   English   中英

“收集新標簽”而不是讀取應用程序的標簽 - NFC android

[英]“New Tag Collected” instead of reading tags of application - NFC android

我有一個類,它創建與 NFC 和兩個活動的連接。 它們都創建了該類的對象,以便它們可以連接到 NFC。 早些時候它以某種方式工作,但現在我遇到了問題 - 我的應用程序在NewIntent上沒有做任何事情,即使在第一個活動中也是如此。 取而代之的是,我可以從名為“標簽”(Nexus S)的內置應用程序中看到“收集的新標簽”。

我應該怎么辦?

班級:

public NFCForegroundUtil(Activity activity)
{
    super();
    this.activity = activity;
    mAdapter = NfcAdapter.getDefaultAdapter(activity
            .getApplicationContext());

    mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(
            activity, activity.getClass())
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndef2 = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter ndef3 = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

    try
    {
        ndef2.addDataType("*/*");
    }
    catch (MalformedMimeTypeException e)
    {
        throw new RuntimeException("fail", e);
    }

    mFilters = new IntentFilter[] {ndef, ndef2, ndef3 };

    mTechLists = new String[][] { new String[] {
            // android.nfc.tech.NfcV.class.getName(),
            android.nfc.tech.NfcA.class.getName(),
            android.nfc.tech.IsoDep.class.getName() } };

    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

}

活動一:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nfcForegroundUtil = new NFCForegroundUtil(this);

}

 @Override
protected void onNewIntent(Intent intent)
{

    super.onNewIntent(intent);
    Intent i = new Intent(this, NfcDisplayLabelActivity2.class);
    startActivity(i);

 }

轉到設置-> 應用程序-> 全部-> 標簽(在我的情況下)-> 禁用

嘗試從 NFC 標簽打開我的應用程序時,我遇到了類似的問題。 我已經在我的 AndroidManifest.xml 中為“magicnfc”方案注冊了一個意圖過濾器,但它打開了 Android OS Tags 應用程序而不是我的。

我發現 NFC 意圖(在我的例子中是 TECH_DISCOVERED)比基於通用方案的意圖過濾器具有更高的優先級。 因為標簽應用注冊了 TECH_DISCOVERED,它被打開而不是我的。

幸運的是,應用程序可以注冊 NDEF_DISCOVERED(一個更高優先級的過濾器)並被打開而不是標簽應用程序。

當我點擊標簽時,這使我的應用程序打開。

更多信息在這里: http : //developer.android.com/guide/topics/connectivity/nfc/nfc.html

但是我發現我必須重寫 onNewIntent 函數,代碼如下:

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
    String uri = intent.getDataString();

    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    if (rawMsgs != null) {
        msgs = new NdefMessage[rawMsgs.length];
        for (int i = 0; i < rawMsgs.length; i++) {
            msgs[i] = (NdefMessage) rawMsgs[i];
        }
    }
}

對我來說,我只需要:

String uri = intent.getDataString();

祝你好運!

您可以監聽使用ACTION_TAG_DISCOVERED意圖激活的所有標簽,而不是使用以下代碼過濾特定標簽:

public NFCForegroundUtil(Activity activity)
{
    super();
    this.activity = activity;
    mAdapter = NfcAdapter.getDefaultAdapter(activity
            .getApplicationContext());

    mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(
            activity, activity.getClass())
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 
0);

    // See below
    mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}

來自NFCAdapter 文檔

如果您為過濾器和 techLists 參數傳遞 null 作為通配符並將導致前台活動通過 ACTION_TAG_DISCOVERED 意圖接收所有標簽。

你的問題是當你初始化意圖 i onNewIntent

這個類應該是它自己,而不是第二個類。

正確的代碼應該是:

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    Intent i = new Intent(this, NfcDisplayLabelActivity1.class);
    startActivity(i);
 }

我從名為“標簽”的內置應用程序中看到“新標簽收集”,因為我的應用程序無法正常工作。

當它工作正常時,它的優先級高於“標簽”,並且手機會從我的應用程序中讀取標簽。 但是當它工作不正常並且手機收集標簽時,“標簽”應用程序被激活並且“標簽”應用程序與我的設備對話。

修復代碼后,我的應用程序具有更高的優先級,手機使用我的應用程序讀取標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM