【问题标题】:"New Tag Collected" instead of reading tags of application - NFC android“收集新标签”而不是读取应用程序的标签 - NFC android
【发布时间】:2012-09-19 05:01:36
【问题描述】:

我有一个类,它创建与 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);

 }

【问题讨论】:

    标签: java android tags nfc nexus-s


    【解决方案1】:

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

    【讨论】:

    • 这不是解决方案。他应该在他的技术列表中过滤适当的标签,或者使用 adapter.enableForegroundDispatch(this, pendingIntent, null, null);当应用程序处于前台时,如下面的一个很好的答案所示
    【解决方案2】:

    我在尝试从 NFC 标签打开我的应用程序时遇到了类似的问题。我在我的 AndroidManifest.xml 中为方案“magicnfc”注册了一个 intentfilter,但它打开了 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();
    

    祝你好运!

    【讨论】:

      【解决方案3】:

      您可以侦听使用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 Documentation

      如果您为过滤器和 techLists 参数都传递了 null ,它们会充当通配符,并导致前台活动通过 ACTION_TAG_DISCOVERED 意图接收所有标签。

      【讨论】:

        【解决方案4】:

        你的问题是当你初始化意图时我 onNewIntent

        类应该是它本身,而不是第二类。

        正确的代码应该是:

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

        【讨论】:

          【解决方案5】:

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

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

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

          【讨论】:

          • 如果仍有问题,请查看我的回答。
          • 这个答案确实没有任何有用的数据。简单地说“哦,我更正了我的应用程序,现在它可以工作了”并不能告诉社区任何关于如何应用程序被更正以及什么你最初做错了。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-26
          • 1970-01-01
          • 2014-12-03
          相关资源
          最近更新 更多