用户更改位置设置后刷新 activity

Refreshing activity after user changed location settings

用户流程:点击一个按钮,被重定向到显示 google 地图的 MapActivity。给定一个位置,有一个按钮可以使用当前位置创建到给定位置的路线。当位置服务关闭时,系统会提示用户将其打开。

private void goToLocationSettings(){
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS );
    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 1) {
        switch (requestCode) {
            case 1: Log.e("test", "onActivityResult");
                break;
        }
    }
}

当用户returns该功能应该可以完成。但程序再次打开设置。日志从未显示。

如果我在我的设备上打开位置服务后稍等片刻,我没有收到打开它的问题,但仍然没有显示日志消息。

我不知道我做错了什么。

您正在检查错误 resultCode,为避免混淆,请使用常量 Activity#RESULT_OK and Activity#RESULT_CANCELED。如果您查看文档,您可以看到 RESULT_OK 的 int 值为 -1。

在启动 Location Settings Activity 结果的情况下,resultCode 将始终为 0,因为用户使用后退按钮退出设置 Activity,因此对系统他好像取消了请求。

static final int LOCATION_SETTINGS_REQUEST = 1;

 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == LOCATION_SETTINGS_REQUEST) {
         // user is back from location settings - check if location services are now enabled
         checkGPS();
     }
 }

没有结果意图的对话框可以像这样我们需要允许用户导航和检查位置服务

已接受

在为结果启动位置设置 Activity 的情况下,resultCode 将始终为 0,因为用户使用后退按钮退出设置 Activity,因此对于系统来说,他取消了请求。

此处的代码允许 dilog 导航到设置页面。

public void showLocationAlert() {
    if (!ismctLocationEnabled(this)) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(ContentTransferBaseActivity.this);
        alertDialog.setTitle(R.string.mct_enable_Location_title);
        alertDialog.setMessage(R.string.mct_enable_location_message);
        alertDialog.setCancelable(true);

        alertDialog.setPositiveButton(R.string.mct_button_Enable,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);
                    }
                });

        alertDialog.setNegativeButton(R.string.mct_button_Dismiss,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        if(!ismctLocationEnabled(this)) {
            alertDialog.show();
        }else
        {
            AlertDialog dialog=alertDialog.create();
            dialog.dismiss();
        }

    }
}

public static boolean ismctLocationEnabled(Context context) {
    int locationMode = 0;
    String locationProviders;

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {

        try {
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }

        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    } else {
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }
}