java “收集新标签”而不是读取应用程序的标签 - NFC android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12620880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:35:48  来源:igfitidea点击:

"New Tag Collected" instead of reading tags of application - NFC android

javaandroidtagsnfcnexus-s

提问by Doszi89

I have a class, which creates conncection to NFC and two activites. Both of them creates an object of that class so they can connect to NFC. Earlier it worked somehow but now I've got problem - my application doesn't do anything onNewIntent, even on the first activity. Instead of it, I can see "New tag collected" from build-in app called "Tags" (Nexus S).

我有一个类,它创建与 NFC 和两个活动的连接。它们都创建了该类的对象,以便它们可以连接到 NFC。早些时候它以某种方式工作,但现在我遇到了问题 - 我的应用程序在NewIntent上没有做任何事情,即使在第一个活动中也是如此。取而代之的是,我可以从名为“标签”(Nexus S)的内置应用程序中看到“收集的新标签”。

What should I do?

我该怎么办?

class:

班级:

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

}

activity 1:

活动一:

@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);

 }

采纳答案by Doszi89

I was seeing "New tag collected" from build-in app called "Tags" because my application didn't work properly.

我从名为“标签”的内置应用程序中看到“新标签收集”,因为我的应用程序无法正常工作。

When it works ok, it has higher priority than "Tags" and phone reads tags from my application. But when it works unproperly and phone collect a tag, "Tags" application is activated and "Tags" application talks to my device.

当它工作正常时,它的优先级高于“标签”,手机会从我的应用程序中读取标签。但是当它工作不正常并且手机收集标签时,“标签”应用程序被激活并且“标签”应用程序与我的设备对话。

After repairing code, my app has higher priority and phone reads tags using my application.

修复代码后,我的应用程序具有更高的优先级,手机使用我的应用程序读取标签。

回答by Rajkumar

Go to settings -> Apps -> All -> Tags(in my case) -> disable

转到设置-> 应用程序-> 全部-> 标签(在我的情况下)-> 禁用

回答by MagicSeth

I had a similar problem when trying to open my app from an NFC tag. I had registered an intentfilter in my AndroidManifest.xml for the scheme "magicnfc" and yet it opened the Android OS Tags app instead of mine.

尝试从 NFC 标签打开我的应用程序时,我遇到了类似的问题。我已经在我的 AndroidManifest.xml 中为“magicnfc”方案注册了一个意图过滤器,但它打开了 Android OS Tags 应用程序而不是我的。

I discovered that the NFC intent (TECH_DISCOVERED in my case) had higher priority than a generic scheme-based intent filter. Because the Tags app registered TECH_DISCOVERED, it was getting opened instead of mine.

我发现 NFC 意图(在我的例子中是 TECH_DISCOVERED)比基于通用方案的意图过滤器具有更高的优先级。因为标签应用注册了 TECH_DISCOVERED,它被打开而不是我的。

Luckily, apps can register for NDEF_DISCOVERED (a higher priority filter) and get opened instead of the Tags app.

幸运的是,应用程序可以注册 NDEF_DISCOVERED(更高优先级的过滤器)并被打开而不是标签应用程序。

That made my app open when I tapped the tag.

当我点击标签时,这使我的应用程序打开。

More info is here: http://developer.android.com/guide/topics/connectivity/nfc/nfc.html

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

But I found that I had to override the function onNewIntent, with code like this:

但是我发现我必须重写 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];
        }
    }
}

For me, all I needed was:

对我来说,我只需要:

String uri = intent.getDataString();

Good luck!

祝你好运!

回答by jwir3

You can listen for alltags activated using the ACTION_TAG_DISCOVEREDintent, rather than filtering for a specific one with the following code:

您可以侦听使用意图激活的所有标签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);
}

From the NFCAdapter Documentation:

来自NFCAdapter 文档

If you pass null for both the filters and techLists parameters that acts a wild card and will cause the foreground activity to receive all tags via the ACTION_TAG_DISCOVERED intent.

如果您为过滤器和 techLists 参数传递 null 作为通配符并将导致前台活动通过 ACTION_TAG_DISCOVERED 意图接收所有标签。