简体   繁体   English

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

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

I have a class, which creates conncection to NFC and two activites.我有一个类,它创建与 NFC 和两个活动的连接。 Both of them creates an object of that class so they can connect to NFC.它们都创建了该类的对象,以便它们可以连接到 NFC。 Earlier it worked somehow but now I've got problem - my application doesn't do anything onNewIntent, even on the first activity.早些时候它以某种方式工作,但现在我遇到了问题 - 我的应用程序在NewIntent上没有做任何事情,即使在第一个活动中也是如此。 Instead of it, I can see "New tag collected" from build-in app called "Tags" (Nexus S).取而代之的是,我可以从名为“标签”(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);

 }

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

I had a similar problem when trying to open my app from an NFC tag.尝试从 NFC 标签打开我的应用程序时,我遇到了类似的问题。 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.我已经在我的 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.我发现 NFC 意图(在我的例子中是 TECH_DISCOVERED)比基于通用方案的意图过滤器具有更高的优先级。 Because the Tags app registered TECH_DISCOVERED, it was getting opened instead of mine.因为标签应用注册了 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!祝你好运!

You can listen for all tags activated using the ACTION_TAG_DISCOVERED intent, 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 意图接收所有标签。

Your problem is when you initialise the intent i onNewIntent你的问题是当你初始化意图 i onNewIntent

The class should be itself, not the second class.这个类应该是它自己,而不是第二个类。

The right code should be :正确的代码应该是:

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

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.修复代码后,我的应用程序具有更高的优先级,手机使用我的应用程序读取标签。

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

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