如何编写 * .kl 文件并复制到根系统上的 / 系统

How to write a * .kl file and copy to / system on a rooted system

我有以下问题。

我需要为特定应用锁定我的平板电脑。我在 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");

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

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

谢谢大家

这就是解决方案 Root 设备

        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/");
        **Java.Lang.Runtime.GetRuntime().Exec("su -c chmod 644 /system/usr/keylayout/Generic.kl");
        Java.Lang.Runtime.GetRuntime().Exec("su -c chown system.system /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");

不过您仍然需要重启。