我遇到以下问题。
我需要为特定应用锁定我的平板电脑。我在 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/