PluginRegistry.ActivityResultListener:没有得到结果

时间:2019-01-22 10:10:18

标签: flutter flutter-packages

我正在尝试通过使用用Java编写的Android本机代码为Flutter创建MyBarcodeScanning包。 我的软件包无法正常工作,因为从MyBarcodeScannerPlugin类启动一个Activity,并在完成后(例如,当用户拒绝权限时),父级不会触发ActivityResultListener。

这是父级MyBarcodeScannerPlugin的代码...

public class MyBarcodeScannerPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener {
  /** Plugin registration. */
  private final Activity activity;
  private Result result;

  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(),    "my_barcode_scanner");

    final MyBarcodeScannerPlugin plugin = new MyBarcodeScannerPlugin(registrar.activity());

    channel.setMethodCallHandler(plugin);
    registrar.addActivityResultListener(plugin);
  }

  private MyBarcodeScannerPlugin(Activity activity) {
    this.activity = activity;
  }


  @Override
  public void onMethodCall(MethodCall call, Result result) {

    if (call.method.equals("scan")) {
      this.result = result;
      showBarcodeView(activity);
    } else {
      result.notImplemented();
    }
  }

  private void showBarcodeView(Activity activity) {
    Intent intent = new Intent(activity, MyBarcodeScannerActivity.class);
    activity.startActivityForResult(intent, 100);
  }

  @Override
  public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    // IT DOES NOT FIRE !
    if (requestCode == 100) {
      if (resultCode == Activity.RESULT_OK) {
        this.result.success("");
      } else {
        String errorCode = data.getStringExtra("ERROR_CODE");
        this.result.error(errorCode, null, null);
      }
      return true;
    }
    return false;
  }
}

这是当用户拒绝权限时完成的子活动...

public class MyBarcodeScannerActivity extends AppCompatActivity {
    ...
    ...
    private void finishWithError(String errorCode) {
        Intent intent = new Intent();
        intent.putExtra("ERROR_CODE", errorCode);
        setResult(Activity.RESULT_CANCELED, intent);
        finish();
    }
    ...
    public void onRequestPermissionsResult(int requestCode, String 
permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA:
                if (grantResults.length > 0) {
                    boolean cameraAccepted = grantResults[0] == 
PackageManager.PERMISSION_GRANTED;
                    if (cameraAccepted){
                    }else {
                        finishWithError("PERMISSION_NOT_GRANTED");
                    }
                }
                break;
        }
    }
}

0 个答案:

没有答案