Android Robolectric addResolveInfoForIntent不工作

Android Robolectric addResolveInfoForIntent不工作,android,kotlin,robolectric,firebase-job-dispatcher,Android,Kotlin,Robolectric,Firebase Job Dispatcher,在robolectric 3.3中,我试图让包管理器为QueryInputServices返回正确的值,以便Firebase作业调度器工作 在我的AndroidManifest.xml中,我有: <service android:name="com.jongla.soundmash.service.SoundMashService" android:exported="false"> <intent-filter> <action

在robolectric 3.3中,我试图让包管理器为QueryInputServices返回正确的值,以便Firebase作业调度器工作

在我的AndroidManifest.xml中,我有:

<service
    android:name="com.jongla.soundmash.service.SoundMashService"
    android:exported="false">
    <intent-filter>
      <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
    </intent-filter>
</service>
但是,Firebase仍抛出此错误:

com.firebase.jobdispatcher.ValidationEnforcer$ValidationException: JobParameters is invalid: Couldn't find a registered service with the name
com.jongla.soundmash.service.SoundMashService. Is it declared in the manifest with the right intent-filter?
Firebase项目中的相关代码如下所示。这看起来很简单,所以我不认为这是他们这边的错误

PackageManager pm = context.getPackageManager();
if (pm == null) {
    return getMutableSingletonList("PackageManager is null, can't validate service");
}

final String msg = "Couldn't find a registered service with the name " + service
    + ". Is it declared in the manifest with the right intent-filter?";

Intent executeIntent = new Intent(JobService.ACTION_EXECUTE);
executeIntent.setClassName(context, service);
List<ResolveInfo> intentServices = pm.queryIntentServices(executeIntent, 0);
if (intentServices == null || intentServices.isEmpty()) {
    return getMutableSingletonList(msg);
}

for (ResolveInfo info : intentServices) {
    if (info.serviceInfo != null && info.serviceInfo.enabled) {
// found a match!
return null;
    }
}
PackageManager pm=context.getPackageManager();
如果(pm==null){
返回getMutableSingletonList(“PackageManager为空,无法验证服务”);
}
final String msg=“找不到名为“+服务”的注册服务
+“。是否在清单中使用正确的意图筛选器声明它?”;
Intent executeIntent=新的Intent(JobService.ACTION\u EXECUTE);
setClassName(上下文、服务);
List intentServices=pm.querytentservices(executecontent,0);
if(intentServices==null | | intentServices.isEmpty()){
返回getMutableSingletonList(msg);
}
用于(ResolveInfo:intentServices){
if(info.serviceInfo!=null&&info.serviceInfo.enabled){
//找到一根火柴!
返回null;
}
}

我使用Robolectric是否做错了什么?

回答我自己的问题…调用代码需要在创建时使用,而不是在测试前使用,这样就可以正常工作了

如果您编辑代码以显示Java代码的封闭类和方法,您可以改进您的问题,使其对其他读者有用。代码可能会经过onResume()处理,这会更好,不是吗?
PackageManager pm = context.getPackageManager();
if (pm == null) {
    return getMutableSingletonList("PackageManager is null, can't validate service");
}

final String msg = "Couldn't find a registered service with the name " + service
    + ". Is it declared in the manifest with the right intent-filter?";

Intent executeIntent = new Intent(JobService.ACTION_EXECUTE);
executeIntent.setClassName(context, service);
List<ResolveInfo> intentServices = pm.queryIntentServices(executeIntent, 0);
if (intentServices == null || intentServices.isEmpty()) {
    return getMutableSingletonList(msg);
}

for (ResolveInfo info : intentServices) {
    if (info.serviceInfo != null && info.serviceInfo.enabled) {
// found a match!
return null;
    }
}