测试活动是否已启动服务

时间:2013-08-13 20:16:22

标签: android unit-testing android-service android-testing

如何测试我的活动是否启动了具有特定意图的服务?

我的问题类似于Unit testing Activity.startService() call,但当我使用ActivityUnitTestCase时遇到了action bar problem

以下是我想测试的示例:

@UiThreadTest
public void testAddSongToAvailableList() {
    ListView songList = (ListView) this.getActivity().findViewById(R.id.allSongsList);
    songList.getChildAt(0).findViewById(R.id.btnAddToAvailableList).performClick(); // should start my service

    //assert that my service was started and is doing stuff
}

2 个答案:

答案 0 :(得分:0)

将数据作为捆绑传递给启动活动?

您是否只是简单地测试服务是否已启动,或者您是否想要明确知道哪些活动启动了该服务?

答案 1 :(得分:0)

使用startService方法启动服务时,可以通过方法的返回值确定活动是否已启动。

如果服务成功启动,则返回the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

更多详情:Android Documentation

相关问题