当我启动我的活动时,我希望使用他的gps来获取用户的位置。 如果GPS未打开,我会显示一个祝酒词,要求他打开他的GPS,以便从特定功能中受益。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.enableAutoManage(this, 34992, this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 50,SelectCinemaActivity.this );
// Define the criteria how to select the locatioin provider -> use default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
setContentView(R.layout.fragment1);
requestLocation();
locationChecker(mGoogleApiClient, Activity.this);
...
}
我的requestLocation()函数
public void requestLocation(){
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
latitudeActuelle = location.getLatitude();
longitudeActuelle = location.getLongitude();
try {
initList();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
System.out.println("Location not available");
try {
initListWithoutDistance();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
这里的locationChecker与Toast:
public void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
Log.i("SUCCESS", String.valueOf(status.getStatusCode()));
requestLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
Log.i("RES REQUIRED", String.valueOf(status.getStatusCode()));
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
activity, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i("UNAIVALEBLE", String.valueOf(status.getStatusCode()));
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
try {
initListWithoutDistance();
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
Log.i("IL CLIQUE", "OK");
location = locationManager.getLastKnownLocation(provider);
Log.i("OK CLICK", String.valueOf(location));
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
Log.i("IL CLIQUE", "CANCEL");
try {
initListWithoutDistance();
} catch (JSONException e) {
e.printStackTrace();
}
break;
default:
break;
}
break;
}
}
initList和initListWithoutDistance是2个按距离或名称排序结果的函数。
当GPS处于活动状态时,我没有遇到任何问题。 当GPS关闭时,我点击“确定”按钮,我无法立即获取当前位置。我试图通过调用 location = locationManager.getLastKnownLocation(provider); 来获取事件onLocationChanged,但结果为null。我无法直接调用onLocationChanged函数,因为我没有位置对象。
几秒钟后(有时是2秒,有时是20 ......),会启动事件,并尝试结果。
有没有办法强制执行此事件,并在用户点击“确定”按钮后立即获取当前位置?
答案 0 :(得分:4)
有没有办法强制执行此事件,并在用户点击“确定”按钮后立即获取当前位置?
没有。 GPS本身需要一些时间才能获得初始位置修复。这与Android没什么关系,与GPS有关。