我们能否在仪器测试中测试静态广播接收器,或者只能在单元测试中测试广播接收器

问题描述

我是 Android 及其仪器测试的新手

我在我的 android 清单中注册一个广播接收器,例如

<receiver android:name=".phone.PhoneCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

和 PhoneCallReceiver 类如下

    public class PhoneCallReceiver extends broadcastReceiver {
        @Override
        public void onReceive(final Context context,Intent intent) {
                
             if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
             String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            showAddCallLogScreen();
        }
            });
           
         }
       
     }`

我想测试当呼叫结束时是否在检测测试中显示添加呼叫日志弹出页面,我试图模拟使用以下意图结束的呼叫

我的仪器测试就像

    @Test
    public void testSystemCallLog(){
        Intent intent = new Intent("android.intent.action.PHONE_STATE");
        intent(TelephonyManager.EXTRA_INCOMING_NUMBER,wotkNumber);
        intent(TelephonyManager.EXTRA_STATE,"IDLE");

       LocalbroadcastManager.getInstance(context).sendbroadcast(intent);
}

我也试过这个 InstrumentationRegistry.getInstrumentation().getContext().sendbroadcast(intent);也是

但它也没有用

知道如何在仪器测试中模拟调用或如何在仪器测试中测试广播接收器吗?

或广播接收器只能在单元情况下进行测试?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)