码迷,mamicode.com
首页 > 移动开发 > 详细

flutter调用Android原生logcat打印日志

时间:2019-12-26 19:12:38      阅读:781      评论:0      收藏:0      [点我收藏+]

标签:exec   android   configure   code   color   val   when   pac   argument   


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



 

 

flutter调用Android原生logcat打印日志

标签:exec   android   configure   code   color   val   when   pac   argument   

原文地址:https://www.cnblogs.com/yongfengnice/p/12103732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!