0

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

我的问题类似于Unit testing Activity.startService() call ,但是当我使用 ActivityUnitTestCase 时遇到了操作栏问题。

这是我想测试的示例:

@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
}
4

2 回答 2

0

pass in the data as a bundle to the initiating activity?

Are you simple testing the service is started or do you want to specifically know which activity initiated the service?

于 2013-08-13T20:27:51.067 回答
0

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

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

更多详细信息:Android 文档

于 2013-08-13T20:36:16.373 回答