android – Service.startForeground()在使用ServiceTestCase运行时抛出NullPointerException

This一个已知的令人烦恼的2岁Android漏洞.

我的问题是除了修改Android源代码并再次编译之外,有没有人知道这个问题的解决方法

这是我的代码,为了完成:

提升NPE的My Service子类方法

/** Shows notification of started service */
private void doStartForeground() {

    // Prepare notification
    final NotificationHelper nh = doNotification("Service started");

    // Start foreground
    startForeground(nh.getNotificationId(),nh.getNotification());
}

这是从onCreate()方法覆盖调用的.

和JUnit测试方法

public void test1() throws InterruptedException {
    assertTrue(context != null);
    final Intent service = new Intent();
    service.setComponent(new ComponentName(CoreService.PACKAGE_NAME,CoreService.SERVICE_FULL_NAME));
    IBinder binder = bindService(service);
    assertTrue(binder != null);
}

堆栈跟踪:

java.lang.NullPointerException
at android.app.Service.startForeground(Service.java:631)
at com.blablabla.android.core.CoreService.doStartForeground(CoreService.java:82)
at com.blablabla.android.core.CoreService.onCreate(CoreService.java:149)
at android.test.ServiceTestCase.bindService(ServiceTestCase.java:234)
at com.blablabla.android.core.test.MainTest.test1(MainTest.java:37)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)

服务启动正常,并且在不通过Android的JUnit运行时工作正常.

解决方法

我终于在我的服务中添加一个新的静态字段:
/** JUNIT - this is for testing purposes only */
public static boolean isJUnit = false;

并在onCreate()方法中检查它:

// Start the service as foreground (Android should not kill this
// service)
if (!isJUnit) {
    doStartForeground();
}

然后我从JUnit的setUp()设置这个标志:

@Override
protected void setUp() throws Exception {
    super.setUp();
    // More setup
    MyServiceClass.isJUnit = true;
}

不是最优雅的解决方案,但让我摆脱封锁.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...