使用对话框启用 GPS
Posted
技术标签:
【中文标题】使用对话框启用 GPS【英文标题】:Enable GPS using Dialog Box 【发布时间】:2016-01-22 14:13:39 【问题描述】:我正在尝试通过用户确认对话框来启用 GPS 设置。
这里是代码
public void turnGPSOn()
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled)
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
alertDialogBuilder
.setMessage("GPS is disabled in your device. Enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,
int id)
/** Here it's leading to GPS setting options*/
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
);
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
dialog.cancel();
);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
以上代码将引导用户进入 gps 设置选项。但我在许多应用程序(最近的应用程序)中看到,确认后 gps 设置将自动启用为高精度。
我的问题是我需要在用户确认(对话框“是”)后将 GPS 设置启用为省电模式(或任何其他模式),而无需进入 GPS 设置。
MinSDK:9
TargetSDK:23
BuildToolVer:23.0.1
问候。
【问题讨论】:
供参考,可能会有所帮助 - ***.com/questions/28759454/… 【参考方案1】:您可以为此使用GoogleApiClient
。
private static GoogleApiClient client;
private Location mLastLocation;
同时实现必要的接口。
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
我已经在上面实现了这些interfaces
像这样在OnCreate
中初始化GoogleApiClient
。添加必要的Api
s。
if (client == null)
client = new GoogleApiClient.Builder(mContext)
.enableAutoManage(context, 0, this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
您可以通过在位置请求中设置优先级来设置准确性。
这些是不同的优先级。
LocationRequest.PRIORITY_HIGH_ACCURACY
LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
LocationRequest.PRIORITY_LOW_POWER
LocationRequest.PRIORITY_NO_POWER
在 onConnected 方法中发出位置请求。
GoogleApiClient.ConnectionCallbacks
接口中提供了onConnected方法。
@Override
public void onConnected(Bundle bundle)
final 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);
result = LocationServices.SettingsApi.checkLocationSettings(client, builder.build());
if (result != null)
result.setResultCallback(new ResultCallback<LocationSettingsResult>()
@Override
public void onResult(LocationSettingsResult locationSettingsResult)
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode())
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a optionsDialog.
try
// Show the optionsDialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
if (status.hasResolution())
status.startResolutionForResult(getActivity(), 1000);
catch (IntentSender.SendIntentException e)
// Ignore the 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 optionsDialog.
break;
);
startResolutionForResult()
这个函数显示对话框 无需进入位置设置即可启用 GPS
1000 是 REQUEST_CODE
并在 onActivity Result 中检索位置
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == Activity.RESULT_OK) && (requestCode == 1000))
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client);
Log.e("location", mLastLocation.getLatitude() + ":" + mLastLocation.getLongitude());
【讨论】:
感谢您的帮助。让我检查一下这段代码,但我没有使用谷歌地图,我认为上面的代码会将位置设置为高精度。我的应用程序是一个后台应用程序,它需要关于位置的最少信息。有没有办法自动将位置设置设置为省电模式? 您可以设置Location Request的优先级来设置省电模式。检查编辑的答案 @Boss 很高兴我找到了这个。如果用户按下否,我怎样才能继续弹出对话框? @BogdanDaniel 你可以在onActivityResult
中查看。检查结果代码RESULT_CANCELED
@Boss 我应该在里面放什么? status.startResolutionForResult(getActivity(), 1000);
还是整个 onConnected ?以上是关于使用对话框启用 GPS的主要内容,如果未能解决你的问题,请参考以下文章
启用 GPS 的 LocationSettingsRequest 对话框
启用 GPS 的 LocationSettingsRequest 对话框 - 跳过了 onActivityResult()
LocationSettingsRequest对话框启用GPS - 跳过onActivityResult()