admin
2022-03-12 222df7b655c51992580b832f5e06c6772d27d9d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.demo.library_flutter.message;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.alibaba.fastjson.JSONObject;
 
import java.util.Map;
 
import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.JSONUtil;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodCodec;
import io.flutter.plugin.common.StandardMessageCodec;
 
public class DataMethodChannel extends MethodChannel {
    public DataMethodChannel(BinaryMessenger messenger, Context context, DataListener dataListener) {
        super(messenger, "com.yeshi.video/data", (MethodCodec) StandardMessageCodec.INSTANCE);
        setMethodCallHandler(new MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
 
                switch (call.method) {
                    case "setSharedValue": {
                        String key = call.argument("key");
                        String value = call.argument("value");
                        SharedPreferences sharedPreferences = context.getSharedPreferences("flutter", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString(key, value);
                        editor.commit();
                    }
                    break;
 
                    case "getSharedValue": {
                        SharedPreferences sharedPreferences = context.getSharedPreferences("flutter", Context.MODE_PRIVATE);
                        result.success(sharedPreferences.getString(call.arguments + "", ""));
                    }
                    break;
                    case "removeSharedValue": {
                        SharedPreferences sharedPreferences = context.getSharedPreferences("flutter", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.remove(call.arguments + "");
                        editor.commit();
                    }
                    break;
                    case "getAdConfig":
                        SharedPreferences sharedPreferences = context.getSharedPreferences("flutter", Context.MODE_PRIVATE);
                        result.success(sharedPreferences.getString("adConfig", ""));
                        break;
                    case "getBaseRequestParams":
                        //获取请求参数
                        Map<String, Object> params = (Map<String, Object>) call.arguments;
                        params = dataListener.getBaseRequestParams(params);
                        //转json返回
                        result.success(JSONObject.toJSON(params));
                        break;
                }
 
 
            }
 
 
        });
    }
 
    public interface DataListener {
        public Map<String, Object> getBaseRequestParams(Map<String, Object> params);
    }
}