我开发了一个需要知道位置的应用程序。我试图使用新的谷歌api客户端,但我有一些问题。
1.当我询问位置时,我要求PRIORITY_HIGH_ACCURACY,但我希望用户可以通过仅使用移动网络来节省电池。当我检查省电模式(或在旧设备上禁用GPS)时一切正常。但是当我在带棒棒糖的手机上测试应用程序时,即使关闭所有位置提供商,电池也会被杀死。 (我也注意到,当电池耗尽时,没有收到位置更新)。
2.我测试了一些设备并试图通过移动网络获得位置。当设备接近wifi(但没有连接)时,这工作正常。但在特定设备(索尼xperia M2)中,需要连接到wifi以更新位置。有什么我可以做的吗?是设备的问题还是谷歌播放服务的版本?
我的代码在这里。我定期发起一个待处理的意图来运行服务,以确保位置监听器仍在监听(我在过去的位置监听器断开连接时遇到了一些问题)
public class UpdateLocService extends Service implements GoogleApiClient.ConnectionCallbacks, LocationListener {
public static final String SENDER_ID = "66801";
public static final String devIdToServer = "send dev id to server";
private static LocationListener listener = null;
//private LocationClient locationClient;
private GoogleApiClient mGoogleApiClient;
private static Location lastLocation = null;;
private LocationRequest mLocationRequest;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
Log.d("UpdateLocService", "dienem service ");
locApi();
}
private void locApi() {
if(mGoogleApiClient != null && (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())){
dLog.d("loc.txt","tryed to connect but already connected or trying now");
dLog.d("flow.txt","tryed to connect but already connected or trying now");
}else{
dLog.d("loc.txt","new connection attempt");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
//.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000 * 60);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
public static Location getLocation(){
return lastLocation;
}
@Override
public void onLocationChanged(Location location) {
dLog.d("loc.txt",location.toString());
lastLocation = location;
}
}