【发布时间】:2015-05-20 16:54:57
【问题描述】:
您好,是否可以创建一个可用于从许多活动中获取最后一个位置的融合位置提供程序单例?
此外,在连接和断开连接方面,我将如何在这些活动中维护一个 Google Api 客户端实例。
【问题讨论】:
标签: android fusedlocationproviderapi
您好,是否可以创建一个可用于从许多活动中获取最后一个位置的融合位置提供程序单例?
此外,在连接和断开连接方面,我将如何在这些活动中维护一个 Google Api 客户端实例。
【问题讨论】:
标签: android fusedlocationproviderapi
这是可能的。您将需要实现私有构造函数/ getInstance() 组合 --
/**
* Private constructor.
*/
private GPSPlotter(Context theContext) {
initializeInstance();
initializeFields(theContext);
buildApiClient();
connectClient();
}
/**
* Returns an instance of the GPS Plotter.
*/
public static GPSPlotter getInstance(Context theContext) {
if (gpsPlotterInstance == null)
return new GPSPlotter(theContext);
else
return gpsPlotterInstance;
}
我相当肯定将 Context 作为参数传递给模型对象违反了 Android 约定,但是像这样构建代码应该可以工作。
【讨论】: