【发布时间】:2014-05-14 16:57:57
【问题描述】:
我有一个持续存在的问题,即我的应用在 NFC 标签扫描后向接收活动传递多个意图。
我的应用程序本质上是扫描带有一些数据的 NFC 标签并将其发送到服务器。 一般来说,它工作正常。我知道 NFC 在几厘米的阈值内工作,也许我的问题是用户错误,例如用户在标签周围挥动手机,它可以在几毫秒内扫描两次。
我在 Application 对象中放置了一个计时器。当用户扫描标签时,我启动计时器并将标志设置为 false,在处理 NFC 意图的 Activity 中检查此标志,如果标志为 false,则放弃意图。这(应该)在 10 秒内停止对标签的多次扫描。
我的应用程序用于家庭护理工作,因此用户将扫描客户家中的标签。当他们第一次扫描标签时,它会使用数据和时间戳将他们登录到该客户端。对标签的第二次扫描会将它们注销。
一般情况下,它可以工作,但有时护理人员会在第一个客户处登录和注销,然后在第二个客户处或有时数小时后,护理人员将在前一个客户处重新登录。
我认为正在发生的是护理人员错误地扫描了两次触发 NFC 适配器的标签。第一个意图立即交付,第二个意图在几分钟/小时后交付。
事情是如何流动的。 扫描标签 -> 传递给 Activity 的意图 -> 启动 AppObj 计时器。 将数据写入 sqlite DB -> 启动 IntentService 以将事务发送到服务器(10 秒后取消)。
谁能看到我错过的一个问题,这可以解释为什么重复的 Intent 会在几个小时后交付?
谁能提出一个可以阻止这种情况发生的解决方案?
在活动中如何处理意图是否存在问题?例如,错误的 Activity 生命周期方法中的错误代码?
有没有办法从第二次错误扫描中取消/拦截第二个意图?
基本上,我想要的是 nfcAdapter 可以在一秒钟内触发 10 次,但只有第一次扫描被传送到活动。我怎样才能做到这一点?
我会发布一些代码。
[编辑2]
Toast.makeText(this, "about to test flags", Toast.LENGTH_LONG).show();
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
Toast.makeText(this, "intent is from history", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "intent is not from history", Toast.LENGTH_LONG).show();
}
.
[编辑3]
<activity
android:name=".NfcscannerActivity"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="com.carefreegroup.rr3.QRCODE_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
<activity
android:name=".EntryActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.carefreegroup.rr3.INVALID_CARER_TAG_SCANNED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
。 [编辑4]
Tag tagTest = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndefTag = Ndef.get(tagTest);
try {
Log.e(TAG, "about to test io operations on Ndef Tag");
ndefTag.connect(); // this should already perform an IO operation and should therefore fail if there is no tag
NdefMessage ndefMsg = ndefTag.getNdefMessage(); // this reads the current NDEF message from the tag and consequently causes an IO operation
Log.e(TAG, "tested io operations on Ndef Tag, must be a real Tag!!!!!******!!!!!!");
} catch (Exception e) {
// there is no tag or communication with tag dropped
Log.e(TAG, "tested io operations on Ndef Tag, No Tag there, must be a re-delivery of the old Tag via an intent!!!!!******!!!!!!!!!!!!!!!!!!!!!!");
onResume();
} finally {
try {
ndefTag.close();
} catch (Exception e) {
}
}
【问题讨论】:
-
您的代码没有多大意义(括号不平衡,
} else { Log.e(TAG, "ndef not discovered!!!!!!"); }没有匹配的if ... {。请发布您的正确(并且格式正确)代码。 -
另外,您是只在
onCreate中处理意图,还是在onStart或onResume中执行任何处理? -
@Michael Roland 嗨,我已经发布了我的 onCreate() 的完整 NFC 相关部分。我只在 onCreate 方法中处理意图。
标签: android android-intent nfc