Waze 等应用程序如何以编程方式打开 GPS?
How Some Applications like Waze Turn on GPS programmatically?
我搜索了很多有关以编程方式打开 GPS 的信息,但没有找到任何解决方案。但是有一些应用程序,比如Waze,不是系统应用程序,可以打开GPS!
例如在 Waze 中显示此弹出窗口(如果您的 GPS 关闭!):
然后你点击打开 GPS 显示这个:
GPS 会自动开启!
我们如何才能像 Waze 那样做到这一点?
以下代码将显示您想要的对话框。
添加以下依赖项。
compile 'com.google.android.gms:play-services-location:11.0.4'
在调用此代码之前,请确保您已连接到 googleApiClient
。
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);
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
task.getResult(ApiException.class);
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvable = (ResolvableApiException) exception;
resolvable.startResolutionForResult(YourActivity.this,100);
} catch (IntentSender.SendIntentException e) {
Log.d(TAG, e.getMessage());
} catch (ClassCastException e) {
Log.d(TAG, e.getMessage());
}
break;
}
}
}
});
我搜索了很多有关以编程方式打开 GPS 的信息,但没有找到任何解决方案。但是有一些应用程序,比如Waze,不是系统应用程序,可以打开GPS!
例如在 Waze 中显示此弹出窗口(如果您的 GPS 关闭!):
然后你点击打开 GPS 显示这个:
GPS 会自动开启! 我们如何才能像 Waze 那样做到这一点?
以下代码将显示您想要的对话框。 添加以下依赖项。
compile 'com.google.android.gms:play-services-location:11.0.4'
在调用此代码之前,请确保您已连接到 googleApiClient
。
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);
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
task.getResult(ApiException.class);
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvable = (ResolvableApiException) exception;
resolvable.startResolutionForResult(YourActivity.this,100);
} catch (IntentSender.SendIntentException e) {
Log.d(TAG, e.getMessage());
} catch (ClassCastException e) {
Log.d(TAG, e.getMessage());
}
break;
}
}
}
});