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);
|
}
|
}
|