【问题标题】:How to read NFC tag data globally inside an Android Application如何在 Android 应用程序中全局读取 NFC 标签数据
【发布时间】:2014-12-03 09:33:20
【问题描述】:

我想在我的应用程序中读取 NFC 标签数据。基本上,我有不同的 NFC 标签与要在我的应用程序中执行的不同操作相关联。我知道在Service 中声明IntentFilter 是个坏主意。我不想将其与任何Activity 关联。

这是我的清单:

 <service android:name="com.yo.helpers.NFCReaderHelper"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="http" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
 </service>

这是我的服务类:

@Override
protected void onHandleIntent(Intent intent) {
    Log.d("yoyo", "Service");
    Tag mTag = null;
    if(intent.getAction()!=null)
    {
        if(intent.getAction().equals("android.nfc.action.NDEF_DISCOVERED"))
            Log.d("yoyo", "Intent call");

        mTag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Log.i("tag ID", bytesToHexString(mTag.getId()));            
    }
}

我没有在 onHandleIntent 方法中得到调用。如果我在Activity 中执行相同的操作,那么我会在onNewIntent 方法中收到意图。

【问题讨论】:

    标签: android android-intent android-service nfc intentfilter


    【解决方案1】:

    您无法通过服务(或广播接收器)接收 NFC 发现意图(NDEF_DISCOVEREDTECH_DISCOVEREDTAG_DISCOVERED)。 Android 上的 NFC 被设计为一种用户交互方式,因此,这些意图仅传递给 Activity。

    您可以使用的一个技巧是使用不可见/透明的活动来处理意图。见How to run android program without open the app?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多