zoukankan      html  css  js  c++  java
  • flutter调用Android原生logcat打印日志

    
    
    //2.本地kotlin代码
    class MainActivity : FlutterActivity() {
        companion object {
            const val FLUTTER_ANDROID_LOG_CHANNEL = "flutter_android_log"
        }
    
        override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
            super.configureFlutterEngine(flutterEngine)
            GeneratedPluginRegistrant.registerWith(flutterEngine)
            MethodChannel(flutterEngine.dartExecutor.binaryMessenger, FLUTTER_ANDROID_LOG_CHANNEL)
                    .setMethodCallHandler { call, result ->
                        var tag: String = call.argument("tag") ?: "LogUtils"
                        var message: String = call.argument("msg") ?: "unknown log message"
                        when (call.method) {
                            "logD" -> Log.d(tag, message)
                            "logE" -> Log.e(tag, message)
                        }
                        result.success(null)
                    }
        }
    }

    //2.flutter的dart代码 下划线_表示私有变量
    import 'package:flutter/services.dart';
    class LogUtils {
    static const String _tag = "LogUtils";
    static const _perform = const MethodChannel("flutter_android_log");

    static void d(String message) {
    _perform.invokeMethod("logD", {'tag': _tag, 'msg': message});
    }

    static void e(String message) {
    _perform.invokeMethod("logE", {'tag': _tag, 'msg': message});
    }
    }

    //3.调用
    void _onPush() {
    LogUtils.e("_onPush press");
    }

    注意坑:与native方法有交互时,最好先Stop掉,再Start。热启动很多时候无效,坑死了。。。
    
    


     
  • 相关阅读:
    我要AFO啦好伤感啊
    noip2012~2015刷题小记录
    【20161114模拟赛】
    第5模块闯关CSS练习题
    HTML练习题
    Mysql常用命令行大全
    mysql破解密码安装与基本管理
    python 闯关之路四(下)(并发编程与数据库编程) 并发编程重点
    Python/ selectors模块及队列
    python3 中 Event.wait 多线程等待
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/12103732.html
Copyright © 2011-2022 走看看