如何将FlatterMethodChannel添加到Android Application.class?
如何在不创建颤振插件的情况下将颤振方法通道添加到Android Application.class? 我可以在活动中完成,但如果我在Application.class中添加MethodChannels,我就无法访问它们 安卓: 日志:缺少PluginException(在通道上找不到方法getPreferences的实现…)如何将FlatterMethodChannel添加到Android Application.class?,android,flutter,Android,Flutter,如何在不创建颤振插件的情况下将颤振方法通道添加到Android Application.class? 我可以在活动中完成,但如果我在Application.class中添加MethodChannels,我就无法访问它们 安卓: 日志:缺少PluginException(在通道上找不到方法getPreferences的实现…) 类应用程序:flatterApplication(),PluginRegistry.PluginRegistrantCallback{ 重写fun onCreate(){
类应用程序:flatterApplication(),PluginRegistry.PluginRegistrantCallback{
重写fun onCreate(){
super.onCreate()
setPreferencesChannel()
}
用(注册:PluginRegistry?){
GeneratedPluginRegistrant.registerWith(reg)
}
私人娱乐设置首选项Schannel(){
val channel=MethodChannel(FlatterEngine(this).dartExecutor.binaryMessenger,applicationContext.packageName+“/preferences”)
channel.setMethodCallHandler{调用:MethodCall,结果:MethodChannel.result->
when(call.method){
“getPreferences”->{
val prefs=PreferenceManager.GetDefaultSharedReferences(此)
val map=HashMap()
map[“font\u size”]=prefs.getInt(“font\u size\u main”,0)
结果:成功(map)
}
else->result.notImplemented()
}
}
}}
颤振:
class PreferencesChannel {
static final _channel =
MethodChannel(BuildConfig.packageName + '/preferences');
PreferencesChannel._();
static Future<dynamic> getPreferences() async {
try {
return _channel.invokeMethod('getPreferences');
} on PlatformException catch (e) {
Logger.logError(e.message, 'PreferencesChannel: getPreferences');
return null;
}
}
}
class PreferencesChannel{
静态最终信道=
方法通道(BuildConfig.packageName+'/preferences');
优先权;
静态未来getPreferences()异步{
试一试{
返回_channel.invokeMethod('getPreferences');
}平台上异常捕获(e){
Logger.logError(例如消息'PreferencesChannel:getPreferences');
返回null;
}
}
}
class PreferencesChannel {
static final _channel =
MethodChannel(BuildConfig.packageName + '/preferences');
PreferencesChannel._();
static Future<dynamic> getPreferences() async {
try {
return _channel.invokeMethod('getPreferences');
} on PlatformException catch (e) {
Logger.logError(e.message, 'PreferencesChannel: getPreferences');
return null;
}
}
}