支持1.12版本新的android插件api

This commit is contained in:
weilu 2019-12-26 16:30:33 +08:00
parent d806560cd5
commit fbd6ece45f
4 changed files with 97 additions and 30 deletions

View File

@ -2,30 +2,40 @@ package com.apptreesoftware.barcodescan
import android.app.Activity
import android.content.Intent
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
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 {
var result : Result? = null
class BarcodeScanPlugin(): MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware {
private var result : Result? = null
private var channel: MethodChannel? = null
private var activity: Activity? = null
constructor(activity: Activity?) : this() {
this.activity = activity
}
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())
channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin)
}
if (registrar.activity() != null) {
val plugin = BarcodeScanPlugin(registrar.activity())
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 +44,10 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
}
private fun showBarcodeView() {
val intent = Intent(activity, BarcodeScannerActivity::class.java)
activity.startActivityForResult(intent, 100)
activity?.let {
val intent = Intent(it, BarcodeScannerActivity::class.java)
it.startActivityForResult(intent, 100)
}
}
override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
@ -51,4 +63,30 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
}
return false
}
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(binding.binaryMessenger, "com.apptreesoftware.barcode_scan")
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
val plugin = BarcodeScanPlugin(binding.activity)
channel?.setMethodCallHandler(plugin)
binding.addActivityResultListener(plugin)
}
override fun onDetachedFromActivity() {
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}
override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}
}

View File

@ -16,19 +16,37 @@
android:label="barcode_scan_example"
android:icon="@mipmap/ic_launcher">
<activity
android:name="com.apptreesoftware.barcodescanexample.MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:exported="true"
android:launchMode="singleTop"
android:name="com.apptreesoftware.barcodescanexample.EmbeddingV1Activity"
android:theme="@style/LaunchTheme"
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" />
android:value="true"/>
</activity>
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:name="com.apptreesoftware.barcodescanexample.MainActivity"
android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize">
<!-- Specify that the launch screen should continue being displayed -->
<!-- until Flutter renders its first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />
<!-- Theme to apply as soon as Flutter begins rendering frames -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@android:style/Theme.Black.NoTitleBar"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

View File

@ -0,0 +1,12 @@
package com.apptreesoftware.barcodescanexample
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class EmbeddingV1Activity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
}
}

View File

@ -1,13 +1,12 @@
package com.apptreesoftware.barcodescanexample
import android.os.Bundle
import com.apptreesoftware.barcodescan.BarcodeScanPlugin
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity(): FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
flutterEngine.plugins.add(BarcodeScanPlugin())
}
}