开始解析片段中的结果

Nic*_*uir 12 android android-fragments onactivityresult

在我的应用程序中,如果用户没有打开位置,我会通过对话框提示,然后尝试通过覆盖活动结果返回该结果(可能不正确).

这是一个片段,所以不确定如何改变事物:

这就是我用startResolutionForResult调用它的对话框:

public void checkDeviceLocationIsOn()
        {
            System.out.println("Test running setting request" );
            LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            builder.setAlwaysShow(true); //this is the key ingredient

            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 state = result.getLocationSettingsStates();
                    switch (status.getStatusCode()) {
                        case LocationSettingsStatusCodes.SUCCESS:
                            // All location settings are satisfied.
                            System.out.println("Test setting all fine starting location request" );
                            getLocation();
                            break;
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location settings are not satisfied. But could be fixed by showing the user
                            // a dialog.
                            try {
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                status.startResolutionForResult(getActivity(), LOCATION_SETTINGS_REQUEST_CODE);
                                System.out.println("Test setting not met starting dialog to prompt user" );
                            } catch (IntentSender.SendIntentException e) {
                                // Ignore the error.
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            // Location settings are not satisfied. However, we have no way to fix the
                            // settings so we won't show the dialog.
                            break;
                    }
                }
            });
        }
Run Code Online (Sandbox Code Playgroud)

然后我尝试在下面得到这样的结果(这永远不会被调用):

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
            case LOCATION_SETTINGS_REQUEST_CODE:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        System.out.println("test user has turned the gps back on");
                        getLocation();
                        break;
                    case Activity.RESULT_CANCELED:

                        System.out.println("test user has denied the gps to be turned on");
                        Toast.makeText(getActivity(), "Location is required to order stations", Toast.LENGTH_SHORT).show();
                        break;
                }
                break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助

Bad*_*ane 19

使用startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null); 而不是startResolutionForResult()


Amr*_*Amr 6

要开始解析片段中的结果,您应该使用新的 API

首先,创建 ActivityResultLanucher

private val resolutionForResult =
    registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { activityResult ->
        if (activityResult.resultCode == RESULT_OK)
            //startLocationUpdates() or do whatever you want
        else {
            showMessage("we can't determine your location")
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它

val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
resolutionForResult.launch(intentSenderRequest)
Run Code Online (Sandbox Code Playgroud)


小智 0

也许一个简单的解决方案是使用两个活动,并通过使用清单上的任何对话框主题将其中一个活动显示为对话框

<activity
        android:name=".LocationDialogActivity"
        android:theme="@style/DialogStyle"></activity>
Run Code Online (Sandbox Code Playgroud)

他们只是像您一直使用的那样使用 onActivityResult 。

对话活动

 private void setLocation() {
    Intent intentResult = new Intent();
    intentResult.putExtra("latitude", "valueLat");
    intentResult.putExtra("longitude", "valueLon");
    setResult(RESULT_OK, intentResult);
    finish();
}
Run Code Online (Sandbox Code Playgroud)

主要活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            /*Take your data*/
            latitude = data.getExtras().getString("latitude");
            longitude = data.getExtras().getString("longitude");
        }
}
Run Code Online (Sandbox Code Playgroud)