简体   繁体   English

如何测试/模拟NFC前台调度?

[英]How do I test/simulate NFC foreground dispatching?

I know you can simulate NFC tags simply by creating an intent and starting and activity with it. 我知道您可以简单地通过创建意图并以此来启动和活动来模拟NFC标签。 From my understanding and testing this only works, if you add intent-filters to your manifest. 根据我的理解和测试,只有在清单中添加意图过滤器时,此方法才有效。

I want to simulate tags and dispatch them to an activity/fragment via foreground dispatch only, so starting the activity with intent-filters in the manifest is not an option for me. 我只想模拟标签,并仅通过前台派发将其发送到活动/片段,所以对于清单而言,在清单中使用intent-filters启动活动不是一种选择。

The structure of my program looks like this: Activity -> several Fragments, one of them is interested in NFC tags via foreground dispatching. 我的程序结构如下:活动->多个片段,其中一个片段对通过前台分派的NFC标签感兴趣。 The nfc-fragment has the necessary code for pendingIntent and dispatch-enabling and -disabling. nfc片段具有未决的必要代码,以及pending-enabled和-disabling的代码。 The activity implements the onNewIntent() method, which invokes further handling of the intent via a method in the nfc-fragment, if the nfc-fragment is active. 该活动实现onNewIntent()方法,如果nfc片段处于活动状态,则该调用通过nfc片段中的方法调用意图的进一步处理。

The program works fine, but I need to test the behaviour with automated tests. 该程序工作正常,但我需要使用自动化测试来测试行为。

I've already tried using 我已经尝试使用

final Intent intent = new Intent(NfcAdapter.ACTION_TAG_DISCOVERED);
intent.putExtra(NfcAdapter.EXTRA_ID, "1234567890".getBytes());
solo.getCurrentActivity().startActivity(intent);

but this just gives me an ActivityNotFoundException . 但这只是给我一个ActivityNotFoundException Currently I retrieve the nfc-fragment and call the method to handle the intent manually from inside the test, but this gives me 当前,我检索nfc-fragment并从测试内部调用方法来手动处理意图,但这给了我

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。

since the method involves updating Views and whatnot. 因为该方法涉及更新Views等等。 It works somehow , since I just need to switch to a different activity or fragment and then go back to get the view updates, but I'd like to know if there is a better and cleaner way of doing this. 以某种方式起作用,因为我只需要切换到其他活动或片段,然后返回以获取视图更新,但是我想知道是否有更好,更清洁的方法。

I appreciate your help, let me know if you need more information. 感谢您的帮助,如果需要更多信息,请告诉我。

I assume that solo.getCurrentActivity() refers to the activity that should receive the NFC intent, otherwise you have to adapt the activity class and context to refer to the right values: 我假设solo.getCurrentActivity()引用应接收NFC意图的活动,否则您必须调整活动类和上下文以引用正确的值:

Class activityCls = solo.getCurrentActivity().getClass();
Context packageContext = solo.getCurrentActivity();

Then you create the pending intent (better yet, you could use the PendingIntent that was passed to the enableForegroundDispatch() method): 然后创建挂起的意图(更好的是,您可以使用传递给enableForegroundDispatch()方法的PendingIntent):

PendingIntent pendingIntent = PendingIntent.getActivity(
    packageContext,
    0,
    new Intent(packageContext, activityCls).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
    0);

Set up the parameters of the NFC intent: 设置NFC意图的参数:

String intentAction = NfcAdapter.ACTION_TAG_DISCOVERED;
Tag tag = ...;
byte[] tagId = ...;
NdefMessage ndefMessage = ...;

Prepare the NFC intent: 准备NFC意图:

Intent intent = new Intent();
intent.setAction(intentAction);
intent.putExtra(NfcAdapter.EXTRA_TAG, tag);
intent.putExtra(NfcAdapter.EXTRA_ID, tagId);
if (ndefMessage != null) {
    intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[] { ndefMessage });

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intentAction)) {
        Uri uri = message.getRecords()[0].toUri();
        String mime = message.getRecords()[0].toMimeType();
        if (uri != null) {
            intent.setData(uri);
        } else {
            intent.setType(mime);
        }
    }
}

Send the pending intent using the parametrization set-up above: 使用上面的参数设置发送待处理的意图:

pendingIntent.send(packageContext, Activity.RESULT_OK, intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM