同步和互斥
问题描述:
有2个Android应用程序:App1
与Process1
和Service1
,App2
与Process2
和Service2
。同步和互斥
还有一个应用程序AppManager
,它只是一个独立的服务,称为ManagerService
。 ManagerService
基本上做两件事情:
- 寄存器听众使用手机
- 这些监听器的帮助下获取数据的传感器。 注意:获取数据可能需要一些时间。现在
,具有限定AIDL,我可以ManagerService
结合Service1
并用ManagerService
结合Service2
。 Service1
和Service2
现在都可以调用远程方法A(...)
来请求在ManagerService
中定义的已注册侦听器,然后从ManagerService
中获取保存在列表中的共享传感器数据。
如果现在Service1
被执行远程方法A(...)
从ManagerService
获得传感器数据,例如:
List A(...) {
// ...
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, locationListener);
// ...
return list;
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
SaveLocationToList(location);
}
}
我究竟如何保证Service2
将仅请求只Service1
后,传感器数据读取已经完成在“request-registration-updateList-getList”过程之上?
答
List A(...)
{
// ...
synchronized(this)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, locationListener);
// ...
return list;
}
}
这看起来过于复杂。为什么这三个独立的应用程序,如果它们如此相互依赖? – CommonsWare 2013-03-06 01:31:42
3种不同的应用程序。 – NickMan 2013-03-12 10:42:20
大家好,感谢您为我编辑和格式化这篇文章...:D我已经在这里发布了我的方式。 – NickMan 2013-03-12 10:53:02