android - 如何在 root 系统上编写 *.kl 文件并复制到/system

标签 android xamarin.android adb root kiosk-mode

我遇到以下问题。

我需要为特定应用锁定我的平板电脑。我在 KioskMode 下使用我的应用程序,但是我需要阻止一些按钮,“Switch_app”、“Volume_UP”、“Volume_DOWN”等。

我能够通过访问 ES 文件资源管理器并手动更改文件、保存并重新启动平板电脑来阻止这些按钮。

但是,我想以编程方式更改此文件。

我尝试过以下方法:

        {
            using (StreamReader sr = new StreamReader("/system/usr/keylayout/Generic.kl"))
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Contains("VOLUME"))
                    {
                        line = $"# {line}";

                    }
                    text += line + "\n";
                    System.Console.WriteLine(text);
                }
            }
            CreateFile();

            TransferFile();
        };


    void CreateFile()
    {


        string sdCard = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        string path = Path.Combine(sdCard, "MyFolder/Generic.kl");
        // This text is added only once to the file.
        if (!System.IO.File.Exists(path))
        {
            // Create a file to write to.
            System.IO.File.WriteAllText(path, text);
        }
    }

并将创建的文件传输到/system/usr/Keylayout我使用这个:

Java.Lang.Runtime.GetRuntime().Exec("su -c mount -o rw,remount,rw /system");
Java.Lang.Runtime.GetRuntime().Exec("su -c rm system/usr/keylayout/Generic.kl");
Java.Lang.Runtime.GetRuntime().Exec("su -c mv /storage/emulated/0/MyFolder/Generic.kl system/usr/keylayout/Generic.kl");

当我使用这些命令时,文件会被复制,但是当我重新启动平板电脑时,物理按钮不再起作用。所以我相信这是与删除旧文件和添加新文件有关的一些问题。

非常欢迎任何帮助和想法。

谢谢大家。

最佳答案

如果您使用 sed 的就地编辑,则无需担心所有权、权限等:

GetRuntime().Exec("su 0 mount -o remount,rw /system");
GetRuntime().Exec("su 0 sed -i 's/^[^#]*VOLUME_DOWN/# &/' /system/usr/keylayout/Generic.kl");
GetRuntime().Exec("su 0 sed -i 's/^[^#]*VOLUME_UP/# &/' /system/usr/keylayout/Generic.kl");
GetRuntime().Exec("su 0 sed -i 's/^[^#]*APP_SWITCH/# &/' /system/usr/keylayout/Generic.kl");

不过您仍然需要重新启动。

关于android - 如何在 root 系统上编写 *.kl 文件并复制到/system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574182/

相关文章:

android - 0dp 大小的可组合项甚至可以组合吗

java - .apk 上传成功,但启动 Intent 导致 ClassNotFoundException com.game.demo.helloworld

android - 应用程序可以包含用户可以控制应用程序权限的页面吗?

Android Instrumentation 测试运行程序 : How to filter multiple test annotations

java - (Android/Java) Google Sheets v4 isUserRecoverableError 状态 : NEED_PERMISSION

android - 如何启用和禁用组件?

android - 在 Visual Studio 中为 Xamarin.Form 应用程序生成 Release APK 时出现问题

Xamarin Android Profiler 无法启动 — "Deploy your app again with a newer version of Xamarin Studio"

android - phonegap adb 中的 INSTALL_FAILED_OLDER_SDK

Android:adb:将文件复制到/系统(权限被拒绝)