【问题标题】:How to close LocationSettingsRequest dialog when we enable location from top status bar?当我们从顶部状态栏启用位置时,如何关闭 LocationSettingsRequest 对话框?
【发布时间】:2018-02-23 10:52:38
【问题描述】:

这是我提示用户启用 GPS 定位的示例代码。

 private void showLocationSettingsRequest(Context context) {
    try {
        /* Initiate Google API Client  */
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(SPPlaysetScanActivity.this)
                .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:
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
                            status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            Log.d(TAG, "showLocationSettingsRequest :PendingIntent unable to execute request.");
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Log.d(TAG, "showLocationSettingsRequest :Location settings are inadequate,
                        // and cannot be fixed here. Dialog not created.");
                        break;
                }
            }
        });

    } catch (Exception e) {
        Log.d(TAG, "showLocationSettingsRequest EX:" + e.toString());
    }
}

如果我们从状态栏设置中禁用位置,我们能够获得位置设置对话框,但是当我们打开位置(从顶部状态栏)时,仍然会出现位置对话框。从状态栏设置打开位置时,我需要关闭对话框。

提前致谢!

【问题讨论】:

    标签: android gps location android-statusbar


    【解决方案1】:

    这在您的代码方面是不可能的。 调用:

    status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
    

    您正在打开单独的Activity(来自 Google Play 服务库)。此代码应该足够智能,注册BroadcastReceiver 以更改位置,然后在启用 GPS 的情况下将结果返回到您的Activity

    由于您无法更改此代码,我想您需要做的是在 Google 问题跟踪器网站上写一个更改请求:https://source.android.com/setup/report-bugs

    【讨论】:

      【解决方案2】:

      我不知道我的回答是否适合你,只是意识到用户可以从状态栏启用定位服务,谢谢你的问题让我思考更多......

      我用的定位服务是这样的:

      private void checkLocationService(){
          boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          boolean networkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
          if (!gpsEnabled && !networkEnable) {
              Log.e(TAG, "gps service not available");
              /*show dialog*/
              if (builder == null){
                  builder = new AlertDialog.Builder(mContext);
                  builder.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          Intent settingLocation = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                          startActivityForResult(settingLocation, 101);
                      }
                  });
                  builder.setMessage("Please enable location service!");
                  alertDialog = builder.create();
                  alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                      @Override
                      public void onCancel(DialogInterface dialog) {
                          Log.e(TAG, "cancel tapped");
                          checkLocationService();
                      }
                  });
              }
              alertDialog.show();
      
          }
          else {
              if (ActivityCompat.checkSelfPermission(
                      mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                      ActivityCompat.checkSelfPermission(
                              mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  return;
              }
              Location location = locationManager.getLastKnownLocation(provider);
              onLocationChanged(location);
          }
      }
      

      然后等待活动结果再次验证

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data){
          //Log.e(TAG, String.valueOf(requestCode) + "|" + String.valueOf(resultCode));
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == 101){
              boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              boolean networkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
              if (gpsEnabled || networkEnable){
                  if (mMap != null){
                      if (mapFragment.getView() != null){
                          View myLocationBtn = ((View) mapFragment.getView()
                                  .findViewById(Integer.parseInt("1")).getParent())
                                  .findViewById(Integer.parseInt("2"));
                          myLocationBtn.setClickable(true);
                          myLocationBtn.performClick();
                          //myLocationBtn.setVisibility(View.GONE);
                          /*if (myLocationBtn instanceof ImageView){
                              myLocationBtn.setBackgroundResource(R.mipmap.ic_launchers);
                          }*/
      
                          Log.e(TAG, "locationButton.performClick()");
                      }
      
                  }
              }
              else {
                  checkLocationService();
              }
          }
      
      }
      

      希望这适合你的答案

      【讨论】:

      • 我没有将用户导航到设置屏幕,只是显示 LocationSettingsRequest.Builder 对话框。所以这个答案对我没有帮助。
      【解决方案3】:

      不可能。该对话框不是您的应用程序的一部分。它是 android 系统的一部分,遗憾的是不提供此功能。

      但是,如果您认为您的用户在提示启用位置时可以从状态栏中执行此操作,您可以在此系统对话框之前显示您自己的对话框。这样,您就可以控制该对话框。在您的对话框上按确定将带您进入该系统对话框。如果用户要从状态栏打开位置,他们很可能会在您自己的对话期间执行此操作。然后,您可以隐藏它并且从不显示系统对话框。否则你可以前进并展示它。

      【讨论】:

        猜你喜欢
        • 2021-05-08
        • 1970-01-01
        • 2013-04-05
        • 2011-11-02
        • 1970-01-01
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        相关资源
        最近更新 更多