【问题标题】:Kotlin equivalent of MainActivity.java handling incoming intents from external applications for use in FlutterKotlin 等效于 MainActivity.java 处理来自外部应用程序的传入意图以在 Flutter 中使用
【发布时间】:2021-06-26 03:53:53
【问题描述】:

“如何在 Flutter 中处理来自外部应用程序的传入意图?”文档https://flutter.dev/docs/get-started/flutter-for/android-devs#how-do-i-handle-incoming-intents-from-external-applications-in-flutter 有 Java 编写的 MainActivity 代码。然而,没有 Kotlin 等价物,但我的 Flutter 项目构建了 Kotlin 文件。因此,我只需要以下 Java 代码的 Kotlin 等价物(或者至少尽可能接近它,我相信我可以弄清楚其余部分):

package com.example.shared;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

  private String sharedText;
  private static final String CHANNEL = "app.channel.shared.data";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
      if ("text/plain".equals(type)) {
        handleSendText(intent); // Handle text being sent
      }
    }
  }

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
      GeneratedPluginRegistrant.registerWith(flutterEngine);

      new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
              .setMethodCallHandler(
                      (call, result) -> {
                          if (call.method.contentEquals("getSharedText")) {
                              result.success(sharedText);
                              sharedText = null;
                          }
                      }
              );
  }

  void handleSendText(Intent intent) {
    sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
  }
}

(我可以学习 Java,但到目前为止,我的工作中涉及 Java 的内容为零,因此仅翻译这个文件来学习 Java 似乎效率不高。)

【问题讨论】:

  • 只需将代码粘贴到 IDE 中,它就会自动转换为 Kotlin。 Android Studio 至少提供了这个选项。
  • @che10,谢谢!这给了我一个很好的线索,最终解决了我的问题。

标签: java android flutter kotlin android-intent


【解决方案1】:

感谢@che10 提示使用 Android Studio 的 Jave 到 Kotlin 转换器 (https://developer.android.com/kotlin/add-kotlin#convert),我能够转换为几乎可编译的基线。编译错误足以告诉我我必须做的剩余修饰是什么。结果如下:

package com.example.shared

import android.content.Intent
import android.os.Bundle

import androidx.annotation.NonNull

import io.flutter.plugin.common.MethodChannel
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity : FlutterActivity() {
    private var sharedText: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val intent: Intent = getIntent()
        val action: String? = intent.getAction()
        val type: String? = intent.getType()
        if (Intent.ACTION_SEND == action && type != null) {
            if ("text/plain" == type) {
                handleSendText(intent) // Handle text being sent
            }
        }
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
            .setMethodCallHandler { call, result ->
                if (call.method.contentEquals("getSharedText")) {
                    result.success(sharedText)
                    sharedText = null
                }
            }
    }

    fun handleSendText(intent: Intent) {
        sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
    }

    companion object {
        private const val CHANNEL = "app.channel.shared.data"
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多