Flutter:可以不用close Activity就可以通过MethodChannel从Android模块向Flutter项目发送数据(还在AndroidActivity)?

Flutter: It's possible to send data from Android module to Flutter project via MethodChannel without close Activity(Still in Android Activity)?

我有屏幕。

  1. StoryScreen(来自 Flutter)
  2. MainActivity(来自AndroidActivity,扩展到FlutterActivity)
  3. MainUnityActivity(来自AndroidActivity,扩展到AppCompatActivity)

实际屏幕只有两个,StoryScreenMainUnityActivityMainActivity 仅限主机。

MainActivity中我们定义intentmethod channel.

class MainActivity : FlutterActivity() {
    private val tag = "MAIN"
    private val channel = "NATIVE_EXPERIMENT"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor, channel).setMethodCallHandler { call, result ->
            if (call.method.equals("goToUnityActivity")) {
                val arg = call.arguments as Map<String, Any>
                val gameType = arg.getValue("gameType") as String
                val catalogURL = arg.getValue("catalogURL") as String
                goToUnityActivity(gameType, catalogURL)
                result.success(null)
            } else {
                result.notImplemented()
            }
        }
    }

    private fun goToUnityActivity(gameType: String, catalogURL: String) {
        val intent = Intent(this, MainUnityActivity::class.java)
        intent.putExtra("gameType", gameType)
        intent.putExtra("catalogURL", catalogURL)
        startActivityForResult(intent, MainUnityActivity.REQUEST_CODE_FROM_UNITY)
    }

    private fun notifyFlutterBackFromUnity(data: String?) {
        MethodChannel(flutterEngine?.dartExecutor, channel).invokeMethod("backFromUnity", data)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == MainUnityActivity.REQUEST_CODE_FROM_UNITY) {
            if (resultCode == MainUnityActivity.RESULT_CODE_BACK_FROM_UNITY) {
                val listOfReport = data!!.getStringExtra("listOfReport")
                Log.i(tag, "/// [MainActivity] back from unity --> $listOfReport")
                notifyFlutterBackFromUnity(listOfReport)
            }
        }
    }
}

如果我从 MainUnityActivity 退出,我可以从 Android 发送数据到 Flutter 端,但是如果我们还在 MainUnityActivity 但想从 [=40= 发送数据怎么办? ] 到 Flutter 那边?

class MainUnityActivity : UnityPlayerActivity() {
    private val tag = "MIDDLEWARE_UNITY"
    private val listOfVehicleNames: MutableList<String> = mutableListOf()

    companion object {
        const val RESULT_CODE_BACK_FROM_UNITY = 110
        const val REQUEST_CODE_FROM_UNITY = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        listOfVehicleNames.clear()
    }

    private fun backFromUnity() {
        Log.i(tag, "/// [MainUnityActivity] BackButtonClick")
        val resultIntent = Intent().apply {
            putExtra("listOfReport", listOfVehicleNames.toString())
        }
        setResult(RESULT_CODE_BACK_FROM_UNITY, resultIntent)
        finish()
    }

    override fun BackButtonClick() {
        Log.i(tag, "/// [MainUnityActivity] BackButtonClick")
        backFromUnity()
    }

    override fun ReportButtonClick() {
        Log.i(tag, "/// [MainUnityActivity] ReportButtonClick")
        showDialog()
    }

    private fun notifyFlutterReportFromUnity() {
        val json = """{"title": "JSON Title", "notes": "JSON Notes"}"""
        Log.i(tag, "/// [MainUnityActivity] YesReportClick :$json")
        listOfVehicleNames.add(json)
        // TODO: I want send data to Flutter without close this activity
    }

    private fun showDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("Report")
        builder.setMessage("Is there something wrong ?")

        builder.setPositiveButton(
            "Yes"
        ) { _, _ ->
            Toast.makeText(this, "Okay, we're sorry", Toast.LENGTH_SHORT).show()
            notifyFlutterReportFromUnity()
        }

        builder.setNegativeButton(
            "No"
        ) { _, _ ->
            // User click no
        }

        builder.setNeutralButton("Cancel") { _, _ ->
            // User cancelled the dialog
        }
        builder.show()
    }
}

最后,我使用 MethodChannelEventChannelBroadcastReceiver 修复了它。

Method channel: A named channel for communicating with platform plugins using asynchronous method calls.

Event Channel: A named channel for communicating with platform plugins using event streams.

示例项目在这里:https://github.com/rrifafauzikomara/example_flutter_method_channel