onActivityResult()永远不会被调用

时间:2018-08-28 08:21:27

标签: java android

我的 MainActivity.java 中有一个用作接口的类:

 public class prova{
    prova(){
    }
    @JavascriptInterface
    public void displayGPSRequest() {            
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        Log.i(TAG, "All location settings are satisfied.");
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                        try {
                            // Show the dialog by calling startResolutionForResult(), and check the result
                            // in onActivityResult().
                            status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            Log.i(TAG, "PendingIntent unable to execute request.");
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                        break;
                }
            }

            public void onActivityResult(int requestCode, int resultCode, Intent data)
            {
             //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
                switch (requestCode)
                {
                    case REQUEST_CHECK_SETTINGS:
                        switch (resultCode)
                        {
                            case Activity.RESULT_OK:
                            {
                                // All required changes were successfully made
                                Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show();
                                break;
                            }
                            case Activity.RESULT_CANCELED:
                            {
                                // The user was asked to change settings, but chose not to
                                Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                                break;
                            }
                            default:
                            {
                                break;
                            }
                        }
                        break;
                }
            }
        });
    }
}

在函数displayGPSRequest()中,我有startResolutionForResult()应该调用方法onActivityResult(),但从来没有。我尝试查看其他帖子中使用了片段的帖子,但我并没有真正理解它们。希望您能提供帮助。

1 个答案:

答案 0 :(得分:1)

onActivityResult应该在关联的Activity中重写-当前,您只是在ResultCallback中声明一个永远不会被调用的方法。