【发布时间】: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