mirror of
https://github.com/simplezhli/flutter_barcode_reader.git
synced 2024-11-22 23:09:22 +08:00
avoid use Registrar.activity() before flutter engine is attached to android Activity
this add support for use this plugin in apps with add-to-app feature
This commit is contained in:
parent
39ecc57bc0
commit
2a2a3e0899
@ -2,30 +2,29 @@ package com.apptreesoftware.barcodescan
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.PluginRegistry
|
||||
import io.flutter.plugin.common.PluginRegistry.Registrar
|
||||
|
||||
class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
|
||||
PluginRegistry.ActivityResultListener {
|
||||
class BarcodeScanPlugin(private val registrar: Registrar) : MethodCallHandler, PluginRegistry.ActivityResultListener {
|
||||
var result: Result? = null
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun registerWith(registrar: Registrar): Unit {
|
||||
fun registerWith(registrar: Registrar) {
|
||||
val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan")
|
||||
if (registrar.activity() != null) {
|
||||
val plugin = BarcodeScanPlugin(registrar.activity())
|
||||
val plugin = BarcodeScanPlugin(registrar)
|
||||
channel.setMethodCallHandler(plugin)
|
||||
registrar.addActivityResultListener(plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: Result): Unit {
|
||||
if (call.method.equals("scan")) {
|
||||
override fun onMethodCall(call: MethodCall, result: Result) {
|
||||
if (call.method == "scan") {
|
||||
this.result = result
|
||||
showBarcodeView()
|
||||
} else {
|
||||
@ -34,8 +33,12 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
|
||||
}
|
||||
|
||||
private fun showBarcodeView() {
|
||||
val intent = Intent(activity, BarcodeScannerActivity::class.java)
|
||||
activity.startActivityForResult(intent, 100)
|
||||
if (registrar.activity() == null) {
|
||||
Log.e("BarcodeScanPlugin", "plugin can't launch scan activity, because plugin is not attached to any activity.")
|
||||
return
|
||||
}
|
||||
val intent = Intent(registrar.activity(), BarcodeScannerActivity::class.java)
|
||||
registrar.activity().startActivityForResult(intent, 100)
|
||||
}
|
||||
|
||||
override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
|
||||
|
Loading…
Reference in New Issue
Block a user