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

Android 当我们从顶部状态栏启用位置时,如何关闭LocationSettingsRequest对话框?,android,gps,location,android-statusbar,Android,Gps,Location,Android Statusbar,这是提示用户启用GPS定位的示例代码 private void showLocationSettingsRequest(上下文){ 试一试{ /*启动谷歌API客户端*/ GoogleAppClient GoogleAppClient=新的GoogleAppClient.Builder(SPPlaysetScanActivity.this) .addApi(LocationServices.API) .build(); googleApiClient.connect(); LocationReq

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

private void showLocationSettingsRequest(上下文){
试一试{
/*启动谷歌API客户端*/
GoogleAppClient GoogleAppClient=新的GoogleAppClient.Builder(SPPlaysetScanActivity.this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
LocationRequest LocationRequest=LocationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
locationRequest.setInterval(10000);
locationRequest.SetFastTestInterval(10000/2);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult=LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
最终状态状态=result.getStatus();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
打破
案例位置设置StatusCodes.RESOLUTION_要求:
试一试{
//通过调用startResolutionForResult()显示对话框,并在onActivityResult()中检查结果。
status.startResolutionForResult(SPActivity.this,请求检查设置);
}catch(IntentSender.sendtintentexe){
Log.d(标记“showLocationSettingsRequest:PendingEvent无法执行请求”);
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//Log.d(标签“showLocationSettingsRequest:位置设置不充分,
//无法在此处修复。未创建对话框。“);
打破
}
}
});
}捕获(例外e){
Log.d(标记为“showLocationSettingsRequestEx:+e.toString());
}
}
如果禁用“从状态栏中定位”设置,则可以获得“定位设置”对话框,但当打开“定位”(从顶部状态栏)时,仍然会显示“定位”对话框。当从状态栏设置打开位置时,我需要关闭对话框


提前谢谢

这在代码方面是不可能的。 电话:

您正在打开单独的
活动
(来自Google Play Services库)。此代码应足够智能,用于注册位置更改的
广播接收器
,然后将结果返回到您的
活动
(如果已启用GPS)


由于您无法更改此代码,我想您需要做的是在Google Issue tracker网站上编写更改请求:

不可能。该对话框不是应用程序的一部分。这是安卓系统的一部分,可惜安卓系统没有提供这种功能


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

我不知道我的答案是否适合您,请注意用户可以从状态栏启用位置服务,谢谢您的问题让我思考更多

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

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对话框。所以这个答案对我没有帮助。
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();
        }
    }

}