启用GPS的LocationSettingsRequest对话框-跳过onActivity

我的应用程序的一部分需要位置服务,因此,如果当前关闭了位置信息,则该应用程序将提示用户启用它。这是我的操作方式:(也在此堆栈溢出答案中看到)


LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()

            .addLocationRequest(mLocationRequest);

builder.setAlwaysShow(true);


PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());


result.setResultCallback(new ResultCallback<LocationSettingsResult>() 

{

     @Override

     public void onResult(LocationSettingsResult result) 

     {

         final Status status = result.getStatus();

         final LocationSettingsStates = result.getLocationSettingsStates();

         switch (status.getStatusCode()) 

         {

             case LocationSettingsStatusCodes.SUCCESS:

                 // All location settings are satisfied. The client can initialize location

                 // requests here.

                 ...

                 Log.d("onResult", "SUCCESS");

                 break;

             case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

                 // Location settings are not satisfied. But could be fixed by showing the user

                 // a dialog.

                 Log.d("onResult", "RESOLUTION_REQUIRED");

                 try 

                 {

                     // Show the dialog by calling startResolutionForResult(),

                     // and check the result in onActivityResult().

                     status.startResolutionForResult(OuterClass.this, REQUEST_LOCATION);

                 } 

                 catch (SendIntentException e) 

                 {

                     // Ignore the error.

                 }

                 break

         }

     }

 });

该代码运行良好,但是onActivityResult()始终会被跳过。无论用户按Yes,No或back从Dialog,onActivityResult()不运行。


我需要Android来拨打电话,onActivityResult()因此,如果用户选择不打开位置服务,则可以适当地处理它。


Google的开发人员页面(以及上面的代码)明确表示onActivityResult()应调用该页面。有人知道为什么它被跳过吗?


我也不知道这行的目的是:


final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);


互换的青春
浏览 926回答 3
3回答

30秒到达战场

您需要将此添加到您的结果回调中:case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; fragment.startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null);&nbsp; &nbsp; } catch (IntentSender.SendIntentException e) {&nbsp; &nbsp; &nbsp; &nbsp; // Ignore the error.&nbsp; &nbsp; }&nbsp; &nbsp; break;onActivityResult将在片段上被调用,您无需在活动中手动调用它。本质上是这样startResolutionForResult工作的。

白板的微信

如果您想让结果返回片段而不是使用startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);代替 status.startResolutionForResult(YourActivity, LOCATION_REQUEST);使用上述方法只会将结果传递回您的片段。
打开App,查看更多内容
随时随地看视频慕课网APP