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。热启动很多时候无效,坑死了。。。
    
    


     
  • 相关阅读:
    反向映射和写时复制
    内存分析的好blog
    minicom使用
    tee的妙用
    网络带宽
    mem analyse
    linux 应用层常用API/命令
    ubuntu 库依赖问题
    Python基础学习笔记(一:hello world)
    第7章 取消与关闭
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/12103732.html
Copyright © 2011-2022 走看看