【问题标题】:Robolectric addResolveInfoForIntent not workingRobolectric addResolveInfoForIntent 不起作用
【发布时间】:2017-11-15 01:09:31
【问题描述】:

使用 robolectric 3.3,我试图让包管理器返回正确的 queryIntentServices 值,以便 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>

在我的 Robolectric 调用的测试应用程序类中,我放了:

val executeIntent = Intent("com.firebase.jobdispatcher.ACTION_EXECUTE")
executeIntent.setClassName(RuntimeEnvironment.application, "com.jongla.soundmash.service.SoundMashService")
Shadows.shadowOf(packageManager)
  .addResolveInfoForIntent(executeIntent, ResolveInfo().apply { serviceInfo = ServiceInfo().apply { enabled = true } })

但是,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;
    }
}

我在使用 Robolectric 时做错了吗?

【问题讨论】:

  • 如果您编辑代码以显示 Java 代码的封闭类和方法,您可以改进您的问题以对其他读者有用。

标签: android kotlin robolectric firebase-job-dispatcher


【解决方案1】:

回答我自己的问题...调用代码需要在onCreate不是 beforeTest - 然后它工作就好了!

【讨论】:

  • 可能代码会通过 onResume() 会更好不是吗?
猜你喜欢
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多