|
1 | 1 | package com.plugin.flutter.zsdk; |
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.content.Context; |
4 | 5 |
|
| 6 | +import androidx.annotation.NonNull; |
| 7 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 8 | +import io.flutter.plugin.common.BinaryMessenger; |
5 | 9 | import io.flutter.plugin.common.MethodCall; |
6 | 10 | import io.flutter.plugin.common.MethodChannel; |
7 | 11 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
8 | 12 | import io.flutter.plugin.common.MethodChannel.Result; |
9 | 13 | import io.flutter.plugin.common.PluginRegistry.Registrar; |
10 | 14 |
|
11 | 15 | /** ZsdkPlugin */ |
12 | | -public class ZsdkPlugin implements MethodCallHandler { |
| 16 | +public class ZsdkPlugin implements FlutterPlugin, MethodCallHandler { |
13 | 17 |
|
14 | | - /** Plugin registration. */ |
15 | | - public static void registerWith(Registrar registrar) { new ZsdkPlugin(registrar);} |
| 18 | + @Override |
| 19 | + public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { |
| 20 | + init( |
| 21 | + flutterPluginBinding.getApplicationContext(), |
| 22 | + flutterPluginBinding.getBinaryMessenger() |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { |
| 28 | + if(channel != null) channel.setMethodCallHandler(null); |
| 29 | + } |
| 30 | + |
| 31 | + // This static method is only to remain compatible with apps that don’t use the v2 Android embedding. |
| 32 | + @Deprecated() |
| 33 | + @SuppressLint("Registrar") |
| 34 | + public static void registerWith(Registrar registrar) |
| 35 | + { |
| 36 | + new ZsdkPlugin().init( |
| 37 | + registrar.context(), |
| 38 | + registrar.messenger() |
| 39 | + ); |
| 40 | + } |
16 | 41 |
|
17 | 42 | /** Channel */ |
18 | 43 | static final String _METHOD_CHANNEL = "zsdk"; |
@@ -42,15 +67,18 @@ public class ZsdkPlugin implements MethodCallHandler { |
42 | 67 | private MethodChannel channel; |
43 | 68 | private Context context; |
44 | 69 |
|
45 | | - public ZsdkPlugin(Registrar registrar) |
| 70 | + public ZsdkPlugin() { |
| 71 | + } |
| 72 | + |
| 73 | + private void init(Context context, BinaryMessenger messenger) |
46 | 74 | { |
47 | | - context = registrar.context(); |
48 | | - channel = new MethodChannel(registrar.messenger(), _METHOD_CHANNEL); |
| 75 | + this.context = context; |
| 76 | + channel = new MethodChannel(messenger, _METHOD_CHANNEL); |
49 | 77 | channel.setMethodCallHandler(this); |
50 | 78 | } |
51 | 79 |
|
52 | 80 | @Override |
53 | | - public void onMethodCall(MethodCall call, Result result) { |
| 81 | + public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
54 | 82 | try |
55 | 83 | { |
56 | 84 | ZPrinter printer = new ZPrinter( |
|
0 commit comments