Android 使用ADB发送消息的编码问题

Android 使用ADB发送消息的编码问题,android,encoding,adb,Android,Encoding,Adb,我已经实现了一个监听通过ADB发出的命令的服务。通过ADB发送的命令示例如下所示: adb shell am startservice-a com.testandroid.SEND_SMS-电子号码123123-电子信息“åäö” 现在,这里的问题是字符串“åäö”的编码似乎出错了。如果我将该字符串附加并立即将其输出到日志,则会得到一个方形“[]”,未知字符。如果我发送此消息,我会在消息应用程序中获得中文字符。只要我坚持使用非umlaut字符(我想是ASCII),一切正常 我使用的是Window

我已经实现了一个监听通过ADB发出的命令的服务。通过ADB发送的命令示例如下所示:

adb shell am startservice-a com.testandroid.SEND_SMS-电子号码123123-电子信息“åäö”

现在,这里的问题是字符串“åäö”的编码似乎出错了。如果我将该字符串附加并立即将其输出到日志,则会得到一个方形“[]”,未知字符。如果我发送此消息,我会在消息应用程序中获得中文字符。只要我坚持使用非umlaut字符(我想是ASCII),一切正常

我使用的是Windows7和命令行。我没有涉及命令行的编码,我尝试通过获取字节字符来处理额外字符串,将UTF-8作为编码参数传入,然后创建一个新字符串,并将UTF-8作为编码参数传入。不过,不要掷骰子

使用getBytes()时,字节的值为å:-27,ä:-92,ö:-74

我怎样才能让它玩得很好,这样我至少可以利用umlauts


所有这些在Linux中都能很好地工作。

最后,问题可能出在cmd.exe或adb.exe中。除非其中一个或两个都更新为更加兼容,否则我暂时无法使用它。

我遇到了相同的问题,但最终我成功了

如果使用示例C#,则必须按照以下示例进行操作:

2019年12月2日

根据协议,ADB协议支持“智能插座”。这些套接字可以用来完成所有的工作,ADB.exe中的ADB客户端可以完成这些工作。例如,如果你想上传一个文件,你必须请求这样一个“智能插座”。之后,您必须遵循分配给服务的协议(有关服务概述,请参阅),如中所述

2014年10月13日

public static List<string> ExecuteBG(string exe, string args, int timeOut = -1)
{
    if (File.Exists(exe) || exe == "cmd.exe")
    {
        ProcessStartInfo StartInfo = new ProcessStartInfo();
        StartInfo.FileName = exe;
        StartInfo.Arguments = Encoding.Default.GetString(Encoding.UTF8.GetBytes(args));
        StartInfo.CreateNoWindow = true;
        StartInfo.UseShellExecute = false;
        StartInfo.RedirectStandardError = true;
        StartInfo.RedirectStandardOutput = true;
        StartInfo.StandardErrorEncoding = Encoding.UTF8;
        StartInfo.StandardOutputEncoding = Encoding.UTF8;
        AutoResetEvent errorWaitHandle = new AutoResetEvent(false);
        AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
        List<string> response = new List<string>();

        Process proc = new Process();
        proc.StartInfo = StartInfo;
        proc.ErrorDataReceived += (s, e) =>
        {
            if (String.IsNullOrEmpty(e.Data))
            {
                errorWaitHandle.Set();
            }
            else
            {
                response.Add(e.Data);
            }
        };
        proc.OutputDataReceived += (s, e) =>
        {
            if (String.IsNullOrEmpty(e.Data))
            {
                outputWaitHandle.Set();
            }
            else
            {
                response.Add(e.Data);
            }
        };
        proc.Start();
        proc.BeginErrorReadLine();
        proc.BeginOutputReadLine();
        proc.WaitForExit(timeOut);
        errorWaitHandle.WaitOne(timeOut);
        outputWaitHandle.WaitOne(timeOut);
        return response;
    }
    return new List<string>();
}
publicstaticlist ExecuteBG(stringexe,stringargs,int timeOut=-1)
{
如果(File.Exists(exe)| | exe==“cmd.exe”)
{
ProcessStartInfo StartInfo=新的ProcessStartInfo();
StartInfo.FileName=exe;
StartInfo.Arguments=Encoding.Default.GetString(Encoding.UTF8.GetBytes(args));
StartInfo.CreateNoWindow=true;
StartInfo.UseShellExecute=false;
StartInfo.RedirectStandardError=true;
StartInfo.RedirectStandardOutput=true;
StartInfo.StandardErrorEncoding=Encoding.UTF8;
StartInfo.StandardOutputEncoding=Encoding.UTF8;
AutoResetEvent errorWaitHandle=新的AutoResetEvent(假);
AutoResetEvent outputWaitHandle=新的AutoResetEvent(假);
列表响应=新列表();
Process proc=新流程();
proc.StartInfo=StartInfo;
proc.ErrorDataReceived+=(s,e)=>
{
if(String.IsNullOrEmpty(e.Data))
{
errorWaitHandle.Set();
}
其他的
{
答复.添加(如数据);
}
};
proc.OutputDataReceived+=(s,e)=>
{
if(String.IsNullOrEmpty(e.Data))
{
outputWaitHandle.Set();
}
其他的
{
答复.添加(如数据);
}
};
proc.Start();
proc.BeginErrorReadLine();
进程BeginOutputReadLine();
进程WaitForExit(超时);
errorWaitHandle.WaitOne(超时);
outputWaitHandle.WaitOne(超时);
返回响应;
}
返回新列表();
}
真正重要的是这部分“StartInfo.Arguments=Encoding.Default.GetString(Encoding.UTF8.GetBytes(args));”,这里我们将UTF8字符串转换为cmd已知的Windows“Default”字符集。因此,我们向cmd发送一个“销毁的”“默认”编码字符串,Android shell将其转换回UTF8。所以我们有“乌姆洛特”,比如“乌洛特”等等

希望这对别人有帮助

PS:如果你需要一个支持UTF8文件/文件夹推/拉的工作“框架”,也可以看看它是C#.NET4编写的

问候,,
Sebastian

好吧,cmd.exe和PowerShell在编码方面存在问题,特别是在传递诸如“adb shell am startservice-a nu.app.DO_MESSAGE-e ints 123-e MESSAGE‘åäöÄÖ’之类的命令时,因为无论如何都不可能正确地显示此字符串。我已经将它缩小到cmd.exe发出adb命令之间的某个位置,而adb命令又将命令传递给我设备上的shell。当我通过键入“adb shell”来“手动”传递字符串时,会打开shell命令提示符,然后发出“am startservice…”命令,一切正常。在将代码页更改为UTF-8后,您是否测试过它是否正常工作?是的,我有。我尝试了几个不同的代码页,看看这是否有任何效果,唉,什么都没有。它总是给我同样的不可读的字符。当我输入“åäÅÅÅÅÅÅØØ”时,shell似乎将其中一些字符转换为控制代码,因为我得到的输出“åØØØØ€=/system/bin/app u process”是错误的。