使用 C# 连接到 android 模拟器 [英] Connect to android emulator with C#

查看:183
本文介绍了使用 C# 连接到 android 模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .apk 包,我想将它推送到 android 模拟器并在模拟器中运行,然后接收事件和调试消息以使用 C# 控制台程序向用户显示.
我应该如何连接到 android 模拟器以从 C# 代码控制台接收调试消息?请帮我.谢谢.

I have an .apk package that i want to push it into android emulator and run in emulator and then receive events and debug message to show to user with C# console program.
How should i connect to android emulator for receiving debug message from C# code console? please help me. Thanks.

推荐答案

这取决于您想要多少细节.您只是想查看 logcat 还是获取完整的调试信息(如断点和设备统计信息)?

This depends on how much detail you want. Do you simply want to view the logcat or get full debug info(like breakpoints and device statistics)?

如果是前者:

您可以使用 adb(Android Debug Bridge):

You can use the adb(Android Debug Bridge):

ProcessStartInfo adbStart = new ProcessStartInfo(@"your_path\android-sdk\platform-tools\adb.exe", "-e logcat *:D");
adbStart.UseShellExecute = false;
Process adb = new Process() { StartInfo = adbStart };
adb.Start();

adbStart 构造函数中的第一个参数替换为您的 adb.exe 的路径,该路径位于 Android SDK 的 platform_tools 文件夹中.第二个参数是控制台标志.上面的代码强制 adb 仅使用 -e 标志连接到模拟器,并仅使用 *:D 标志过滤器调试消息.您应该将 * 替换为您的应用程序的相关过滤器(如包名称).只需在控制台中输入 adb help 即可获取有关不同标志的信息.

Replace the first parameter in the constructor of adbStart with the path to your adb.exe, found in the platform_tools folder of your Android SDK. The second parameter is console flags. The above code forces the adb to only connect to an emulator using the -e flag and filters to debug messages only using the *:D flag. You should replace the * with a relevant filter for your app(like a package name). Just type adb help in a console for info on the different flags.

由于您的程序在控制台中运行,因此 adb 的标准输出应该定向到您的控制台.如果不是,请使用 adb.StandardOutput 中的 StreamReader 读取 adb 的输出.您可以使用 adb.WaitForExit() 来阻塞线程,直到 adb 死亡.

Since your program runs in the console, the standard output of the adb SHOULD be directed to your console. If it isn't, use the StreamReader at adb.StandardOutput to read the output of the adb. You can use adb.WaitForExit() to block the thread until adb dies.

如果是后者:

官方的方法是通过 Dalvik Debug Monitor(android_sdk\tools\ddms.bat).由于 android 中没有对 C# 的官方支持,调试监视器是用 Java 编写的,libs 也是如此.我找不到任何替代品,抱歉.Java 与 C# 非常相似,也许您可​​以为这个特定项目跳转语言?

The official way to do this is via the Dalvik Debug Monitor(android_sdk\tools\ddms.bat). Since there is no official support for C# in android, the debug monitor is written in Java and so are the libs. I can't find any substitutes, sorry. Java is very similar to C# though, maybe you can jump languages for this particular project?

这篇关于使用 C# 连接到 android 模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆