当我们从顶部状态栏启用位置时,如何关闭LocationSettingsRequest对话框?

问题描述 投票:2回答:3

这是我提示用户启用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
3个回答
1
投票

这在代码方面是不可能的。呼叫:

status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);

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

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


1
投票

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

我用于位置服务的是这样的:

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();
        }
    }

}

希望这符合你的答案


1
投票

不可能。该对话框不属于您的应用。它是android系统的一部分,它不能令人遗憾地提供这种功能。

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

© www.soinside.com 2019 - 2024. All rights reserved.