与提示用户进入设置页面并启用位置服务并再次返回的传统方法相反,我注意到在一些最新应用程序中执行相同 *** 作的更简单方法.
参考下面的屏幕截图,它会向用户提示一个对话框,只需点击一下即可启用位置服务,它可以在这些应用中运行.
我怎样才能实现同样的目标?
解决方法:
此对话框由Google Play服务中的LocationSettingsRequest.Builder创建.
您需要为app build.gradle添加依赖项:
compile 'com.Google.androID.gms:play-services-location:10.0.1'
然后你可以使用这个最小的例子:
private voID displayLocationSettingsRequest(Context context) { Googleapiclient Googleapiclient = new Googleapiclient.Builder(context) .addAPI(LocationServices.API).build(); Googleapiclient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(10000); locationRequest.setFastestInterval(10000 / 2); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); builder.setAlwaysShow(true); PendingResult<LocationSettingsResult> result = LocationServices.SettingsAPI.checkLocationSettings(Googleapiclient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @OverrIDe public voID onResult(LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsstatusCodes.SUCCESS: Log.i(TAG, "All location settings are satisfIEd."); break; case LocationSettingsstatusCodes.RESolUTION_required: Log.i(TAG, "Location settings are not satisfIEd. Show the user a dialog to upgrade location settings "); try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { Log.i(TAG, "PendingIntent unable to execute request."); } break; case LocationSettingsstatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created."); break; } } });}
你可以找到完整的例子here.
总结以上是内存溢出为你收集整理的android – 打开位置服务,而无需导航到设置页面全部内容,希望文章能够帮你解决android – 打开位置服务,而无需导航到设置页面所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)