Geo*_*lov 13 java gps android geolocation fusedlocationproviderapi
所以我已经实现了新的Fused Location Provider API以获取用户的位置,但由于某种原因,除非GPS打开,否则我无法获得任何位置.并非总是如此,用户是否会打开GPS,我不想让他们每次加载应用程序时都要打开GPS. 如何告诉API为我提供一个位置,其中包含可用的提供商?
这是我的代码:
public class FusedLocationService implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
public interface OnLocationChangedListener {
public void OnLocationChanged(Location location);
}
private final String TAG = "SE8611";
private boolean mRequestingLocationUpdates = true;
private OnLocationChangedListener mCallBack;
Service locationService;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private Location mCurrentLocation;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
public FusedLocationService(Service locationService, final long INTERVAL, final long FASTEST_INTERVAL) {
Logger.log(TAG, "FusedLocationService called");
this.mCallBack = (OnLocationChangedListener)locationService;
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
this.locationService = locationService;
googleApiClient = new GoogleApiClient.Builder(locationService)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (googleApiClient != null) {
googleApiClient.connect();
}
}
@Override
public void onConnected(Bundle connectionHint) {
if(mRequestingLocationUpdates) {
startLocationUpdates();
}else{
Logger.log(TAG, "Location updates are already running.");
}
}
protected void startLocationUpdates() {
this.fusedLocationProviderApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
this.mRequestingLocationUpdates = false;
}
@Override
public void onLocationChanged(Location mCurrentLocation) {
Logger.log(TAG, "onLocationChanged called");
this.mCurrentLocation = mCurrentLocation;
this.mCallBack.OnLocationChanged(this.mCurrentLocation);
}
public void startLocationUpdatesAfterResume(){
if (googleApiClient.isConnected() && !mRequestingLocationUpdates) {
Logger.log(TAG, "startLocationUpdatesAfterResume called");
this.startLocationUpdates();
}
}
public void stopLocationUpdates() {
Logger.log(TAG, "stopping Location Updates");
LocationServices.FusedLocationApi.removeLocationUpdates(
googleApiClient, this);
}
public Location getLocation() {
return this.mCurrentLocation;
}
@Override
public void onConnectionSuspended(int i) {
this.mRequestingLocationUpdates = true;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
this.mRequestingLocationUpdates = true;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
我和你一样有同样的问题,这根本不是问题.Android曾经有一个GPS按钮,可以让你直接控制它,但他们用一个不同的位置按钮取而代之.要获得任何类型的位置,您必须打开它.和你一样,我认为位置按钮只能打开和关闭GPS,但事实并非如此.您可以通过更改位置模式来控制GPS:1.高精度(GPS,Wi-Fi和移动网络)2.省电(Wi-Fi和移动网络)3.仅限GPS
| 归档时间: |
|
| 查看次数: |
6608 次 |
| 最近记录: |