是否知道是否有任何方法从MainActivity启动服务,发送一些数据以及服务完成时,从服务中获取数据。例如:Android从MainActivity的服务中获取数据
//MainActivity.class
private ArrayList myList = new ArrayList();
public void fetchDataFromInternet()
{
Intent intent = new Intent(this, DataFromInternetService.class);
intent.putArrayListExtra("MYLIST", myList);
startService(intent);
//In DataFromInternetService the app fetches data from the internet and puts them in the myList.
/*
TODO:
1. Get the data from the service after the it has finished.
*/
}
//Note: The service runs only 1 time.
而且,如果这是不可能的,你有什么建议可供选择,其中MainActivity访问从DataFromInternetService.class数据。
注意:我不想使用LoaderManager,我使用Services来执行不影响UI的后台任务。
您可以使用本地广播接收器。 –
是的,但服务完成后我将如何获得新的“myList”? – user6115152
您在完成服务时意味着什么?那么你可以通过广播接收机的意图发送列表。 –