融合位置提供者单例示例?
Fused location provider singleton example?
您好,是否可以创建一个可用于从许多活动中获取最后位置的融合位置提供程序单例?
此外,我将如何在连接和断开连接方面跨这些活动维护 Google Api 客户端实例。
有可能。您将要实现私有构造函数/ 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 约定,但像这样构建代码应该可行。
您好,是否可以创建一个可用于从许多活动中获取最后位置的融合位置提供程序单例?
此外,我将如何在连接和断开连接方面跨这些活动维护 Google Api 客户端实例。
有可能。您将要实现私有构造函数/ 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 约定,但像这样构建代码应该可行。