【问题标题】:Multiple scans/intents delivered to receiving Activity交付给接收活动的多个扫描/意图
【发布时间】: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


【解决方案1】:

根据您的描述,我假设您受影响的用户通过按主页键“关闭”(实际上隐藏)该应用程序。后来他们从历史记录中打开应用程序(或者更确切地说是那个活动)(长按主页键)。

假设您仅在 onCreate() 中处理与 NFC 相关的意图,您通常不会遇到您在从历史记录中启动活动时描述的问题,因为 onCreate() 方法不会如果您的活动仍然存在于后台,则被调用。 但是如果您的活动同时被破坏(例如应用被手动杀死,应用被系统杀死以释放资源等),从历史记录中重新启动应用将导致您的@987654323 @ 方法再次运行。由于 Android 会在从历史记录中打开应用程序时重建 Activity 堆栈并再次重新发送 原始 Intents(即最初启动您的 Activity 的 Intent),因此您的 Activity 将收到它在它最初是启动的。

您可以通过检查您收到的意图是否设置了FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 来解决此问题。 只有如果您的活动是从历史记录开始的,情况就会如此。

if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
     if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
         NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        ...
    }
}

不解决您最初的问题,但作为最后的手段,请记住,在收到意图后,IO 操作可以访问 NFC 标签。所以为了检查intent中收到的tag是否真的存在,可以执行这样一个IO操作。

例如,如果您的标签是 NDEF 标签:

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndefTag = Ndef.get(tag);

try {
    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
} catch (Exception e) {
    // there is no tag or communication with tag dropped
} finally {
    try {
        ndefTag.close();
    } catch (Exception e) {
    }
}

对于非 NDEF 标签,您需要知道如何使用低级命令访问标签。但至少连接测试适用于任何标签。

【讨论】:

  • 我已经和我的客户谈过了,他说他的护理人员按照您的建议通过 Home 键离开应用程序。但是,他们不会长按 Home 键来启动应用程序,而是单击应用程序图标来启动它。我不确定这是否有影响?
  • Rowland 如果您愿意,我可以将完整的 Activity 放在我公司的服务器上供您下载?
  • @turtleboy 通过启动器图标重新启动活动可能具有相同的效果——尽管我不确定在这种情况下是否会设置 FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 标志。但是,您应该能够通过在 AndroidManifest 中将活动的 android:clearTaskOnLaunch 属性设置为 true 来强制在启动器启动时创建新实例。
  • 罗兰 其他可能发生的情况如下。护理人员在标签“A”处扫描进出。然后她去客户端“B”并扫描标签“B”。该应用程序将标签“A”信息而不是标签“B”信息发送到服务器。另一种情况是护理人员会去客户“A”并扫描进出。然后她会去客户“B”并扫描进出。一段时间后,该应用程序会将标签“A”信息发送到服务器。你对此有什么想法吗?例如,如果她在 4 小时后打开应用程序,那么当保存标签数据的所有变量都设置为 null 并且时,它如何发送标签“A”信息
  • 标签“B”自标签“A”以来已被扫描?
【解决方案2】:

多个扫描/意图传递到接收活动

已接受答案,但更好的选择是

在启动活动时添加以下标志(默认情况下,活动以默认模式启动,即启动新的活动实例) FLAG_ACTIVITY_CLEAR_TOP

【讨论】:

  • 您确实意识到与 NFC 相关的 Intent 是由 Android 系统生成的,并且无法在这些 Intent 中添加该标志?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
相关资源
最近更新 更多