Enable Location/GPS settings without using Play Services API(在不使用 Play Services API 的情况下启用位置/GPS 设置)
问题描述
您可能已经知道,Google Play 服务中有一个新的位置设置对话框 API.
对于不知道的人,这里是使用 Dialog API 的 Maps 应用的屏幕截图.
谷歌只在他们的播放服务中添加了该 API 有点不公平.另一方面,我认为如果他们能做到,就会有一种解决方法来做到这一点.这就是我问这个问题的原因.任何信息表示赞赏.
(注意:有一个漏洞可以让开发人员以编程方式启用和禁用 gps/位置设置.但是 SO 上可用的那些技巧不再有效.请不要发布过时的答案.)
这对我有用.
void initApiClient() {GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addApi(LocationServices.API).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {@覆盖公共无效 onConnected(@Nullable 捆绑包){显示位置设置对话框();}@覆盖公共无效 onConnectionSuspended(int i) {}}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {@覆盖公共无效 onConnectionFailed(@NonNull ConnectionResult 结果){Log.i(TAG, "onConnectionFailed() connectionResult = [" + result + "]");}}).建造();mGoogleApiClient.connect();}无效 showLocationSettingsDialog() {LocationRequest locationRequest = LocationRequest.create();locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();builder.addLocationRequest(locationRequest);builder.setAlwaysShow(true);任务<位置设置响应>结果 = LocationServices.getSettingsClient(getActivity()).checkLocationSettings(builder.build());result.addOnCompleteListener(new OnCompleteListener() {@覆盖公共无效onComplete(任务任务){尝试 {LocationSettingsResponse 响应 = task.getResult(ApiException.class);//满足所有位置设置.客户端可以初始化位置//在这里请求.} 捕捉(ApiException e){开关 (e.getStatusCode()) {案例 LocationSettingsStatusCodes.RESOLUTION_REQUIRED://位置设置不满足.但可以通过显示//用户一个对话框.尝试 {//强制转换为可解决的异常.ResolvableApiException 可解决 = (ResolvableApiException) e;//通过调用 startResolutionForResult() 显示对话框,//并在 onActivityResult() 中检查结果.resolvable.startResolutionForResult(获取活动(),REQUEST_CHECK_SETTINGS);} 捕捉(IntentSender.SendIntentException 异常){//忽略错误.} 捕捉(ClassCastException 异常){//忽略,应该是不可能的错误.}休息;案例 LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE://位置设置不满足.但是,我们没有办法修复//设置所以我们不会显示对话框.休息;}}}});}
对于更多信息 p>
As most of you might already known there is a new Location Settings Dialog API in Google Play Services.
For those who don't know here is the screenshot of Maps app which used Dialog API in action.
It is kinda unfair that google only added that API inside their play services. On the other hand, I think if they could do it, there would be a workaround to do it. That's why I am asking this question. Any information is appreciated.
(Note: There was an exploit which let developers to enable and disable gps/location setting programmatically. However those tricks available on SO no longer works. Please don't post outdated answers.)
hi this works for me.
void initApiClient() {
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
showLocationSettingsDialog();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
Log.i(TAG, "onConnectionFailed() connectionResult = [" + result + "]");
}
})
.build();
mGoogleApiClient.connect();
}
void showLocationSettingsDialog() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getActivity())
.checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) e;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
getActivity(),
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException exception) {
// Ignore the error.
} catch (ClassCastException exception) {
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
});
}
for more info
这篇关于在不使用 Play Services API 的情况下启用位置/GPS 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在不使用 Play Services API 的情况下启用位置/GPS 设置


- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01