Encoding issues using ADB to dispatch messages

2019-07-29 09:06发布

I've implemented a service that listens to commands issued through ADB. An example of a command sent through ADB could look like this:

adb shell am startservice -a com.testandroid.SEND_SMS -e number 123123123 -e message "åäö"

Now, the problem here is that the encoding of the string "åäö" seems to mess up. If I take that string extras and immediately output it to the log, I get a square "[]", unknown character. If I send this message I get chinese characters in the messages app. As long as I stick to non-umlaut characters (ASCII I guess), everything works fine.

I'm using Windows 7 and the command line for this. I have not touched the encoding of the command line and I've tried to process the extras string by getting the byte characters, passing in UTF-8 as an encoding argument, then creating a new String passing in UTF-8 as an encoding argument there as well. No dice, though.

The values of the bytes, when using getBytes() are å: -27, ä: -92, ö: -74

How do I get this to play nice so I can make use of at least the umlauts?

All of this works perfectly fine in Linux.

2条回答
可以哭但决不认输i
2楼-- · 2019-07-29 10:02

i ran into the same issue, but finally i got it work!

if u use for example C# u have to do it like the following example:

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>();
        }

Really important is this part "StartInfo.Arguments = Encoding.Default.GetString(Encoding.UTF8.GetBytes(args));", here we convert the UTF8 string into the Windows "default" charset which is known by cmd. So we send a "destroyed" "default" encoded string to cmd and the Android shell will convert it back to UTF8. So we have the "umlauts" like "üöäÜÖÄàè etc.".

Hope this helps someone.

PS: If u need a working "Framework" which supports UTF8 push/pull for files/folders also have a look at my AndroidCtrl.dll it's C# .NET4 written.

Regards, Sebastian

查看更多
爷的心禁止访问
3楼-- · 2019-07-29 10:04

Concluding, either the problem is situated in cmd.exe or adb.exe. Until either one or both are updated to be more compliant with eachother I will sadly not be able to make use of this for the time being.

查看更多
登录 后发表回答