gpt4 book ai didi

android - Waze 等应用程序如何以编程方式打开 GPS?

转载 作者:行者123 更新时间:2023-12-02 00:16:47 25 4
gpt4 key购买 nike

我搜索了很多有关以编程方式打开 GPS 的信息,但没有找到任何解决方案。但有一些应用程序(例如 Waze)不是系统应用程序,可以打开 GPS!

例如在位智中显示此弹出窗口(如果您的 GPS 关闭!):

enter image description here

然后您单击“打开 GPS”会显示以下内容:

enter image description here

GPS 将自动开启!我们怎样才能像位智那样做到这一点?

最佳答案

下面的代码将显示您想要的对话框。添加以下依赖项。

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;
}
}
}
});

关于android - Waze 等应用程序如何以编程方式打开 GPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48281312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com