首页 > 解决方案 > Android Intent:如何从浏览器开关取回数据

问题描述

我目前正在尝试将 mollie 支付实现到颤振框架中。为此,我想用 Java 构建一个插件。所以 mollie 有一个非常好的文档,如何用 Java 将他们的 API 实现到一个 android 应用程序中。所以问题是当我点击我的应用程序中的付款按钮时,浏览器会打开正确的结帐页面。在客户选择他的首选付款方式后,莫莉回到我的应用程序,但我总是得到一个空查询......

所以这是我的颤振插件代码

public class MolliePlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener {

  Activity activity;
  Result activeResult;
  Context context;
  Intent intent;
  boolean getData;
  private static final int REQUEST_CODE = 0x1337;

  MolliePlugin(Registrar registrar){

    activity = registrar.activity();
  }

  /** Plugin registration. */
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "mollie");
    channel.setMethodCallHandler(new MolliePlugin(registrar));

  }


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

    if (call.method.equals("startPayment")) {

      activeResult = result;
      String checkout = call.argument("checkoutUrl");
      startPayment(checkout);

    }
    else {
      result.notImplemented();
    }

  }


/// Start the browser switch with a ACTION_VIEW
  void startPayment(String checkoutUrl) {

    Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(checkoutUrl));
    activity.startActivity(browserIntent);

  }

因此,在 mollie 的文档中,我必须将以下代码放入 MainActivity 中的 onCreate() 函数中:

public void onCreate(Bundle savedInstanceState)
{
      super.onCreate(savedInstanceState);

      //...

      Intent intent = getIntent();

      if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            String paymentId = uri.getQueryParameter("id");

            // Optional: Do stuff with the payment ID
      }
}

因此,当我将其放入 Flutter 应用程序的 MainActivity 中的 onCreate() 时,在我被路由回我的应用程序后,我总是会得到一个 ACTION_RUN 。所以我使用了 onNewIntent() 函数,它在回到我的应用程序后给了我正确的操作(任何想法为什么?):

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

  }

  @Override
  protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);

    Log.d("Action is: ",intent.getAction());

    String paymentId = "No Id";

    if (Intent.ACTION_VIEW.equals(intent.getAction()) && intent != null) {
      Uri uri = intent.getData();
// This has no data...
      Log.d("query ",intent.getDataString());
      paymentId = uri.getQueryParameter("id");

      // Optional: Do stuff with the payment ID
    }


  }

}

所以在这里我得到一个空查询。intent.getData() 只返回我必须在我的 AndroidManifest 中设置的 returnUrl(见下文)。returnUrl 工作正常,但在签出并切换回应用程序后它没有包含任何数据......

我的安卓清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.plugon.mollie_example">

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="mollie_example"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <data
                    android:host="payment-return"
                    android:scheme="molli" />
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>
    </application>
</manifest>

任何想法我做错了什么?

标签: androidflutterdartmollie

解决方案


推荐阅读