简体   繁体   English

避免在Android中执行shell命令上的“ su”吐司?

[英]Avoid “su” toast on shell command execute in Android?

I'm developing a simple app that injects lines on build.prop by executing a shell command. 我正在开发一个简单的应用,该应用通过执行shell命令在build.prop上插入行。 My main problem is that every time I check a toggle that create the function a toast displaying the shell string appear. 我的主要问题是,每次检查创建该功能的切换开关时,都会出现一个显示外壳字符串的吐司。 Is there any way to avoid this? 有什么办法可以避免这种情况? also, if you have any suggestion to clean a bit the code would be appreciated! 此外,如果您有任何建议要清理一点,代码将不胜感激! (First app for me). (对我来说第一个应用程序)。

Code: 码:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  // fragment not when container null
  if (container == null) {
     return null;
  }
  // inflate view from layout
  View v = (LinearLayout)inflater.inflate(R.layout.performance,container,false);

    final CheckBox hwdebug = (CheckBox) v.findViewById(R.id.hwDebug);   
    final String[] mountrw = {"su","-c","mount -o remount,rw /system"};
    final String[] enhwdebug1 = {"su","-c","sed -i '/debug.sf.hw=*/d' /system/build.prop"};
    final String[] enhwdebug2 = {"su","-c","echo '## Rendering GPU Enabled ##' >> /system/build.prop"};
    final String[] enhwdebug3 = {"su","-c","echo debug.sf.hw=1 >> /system/build.prop"};
    final String[] dishwdebug1 = {"su","-c","sed -i '/debug.sf.hw=1/d' /system/build.prop"};
    final String[] dishwdebug2 = {"su","-c","sed -i '/## Rendering GPU Enabled ##/d' /system/build.prop"};

final SharedPreferences hwdebugpref = this.getActivity().getSharedPreferences("hwdebugck",0);

    // GPU Rendering Checkbox
    boolean hwdebugck = hwdebugpref.getBoolean("hwdebugck", false);
    if (hwdebugck) { 
        hwdebug.setChecked(true);
    } else {
        hwdebug.setChecked(false);
    }
    hwdebug.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {    
        if((hwdebug.isChecked())) {
            SharedPreferences.Editor editor = hwdebugpref.edit();
          editor.putBoolean("hwdebugck", true); // value to store
          editor.commit();   
            ArrayList<String[]> enhwdebug = new ArrayList<String[]>();
            enhwdebug.add(mountrw);
            enhwdebug.add(enhwdebug1);
            enhwdebug.add(enhwdebug2);
            enhwdebug.add(enhwdebug3);
                for(String[] cmd:enhwdebug){
                    try {
                        Runtime.getRuntime().exec(cmd);
                        } catch (IOException e) {
                            e.fillInStackTrace(); 
                        } 
                }
        } else {
          SharedPreferences.Editor editor = hwdebugpref.edit();
          editor.putBoolean("hwdebugck", false); // value to store
          editor.commit();
            ArrayList<String[]> diswdebug = new ArrayList<String[]>();
            diswdebug.add(mountrw);
            diswdebug.add(dishwdebug1);
            diswdebug.add(dishwdebug2);
                for(String[] cmd:diswdebug){
                    try {
                        Runtime.getRuntime().exec(cmd);              
                        } catch (IOException e) {
                            e.fillInStackTrace(); 
                        } 
                } 
                }
    }
});

So, my main problem is that su -c show that annoying toast. 因此,我的主要问题是su -c表示令人讨厌的吐司。 I tried to pass it to busybox or toolbox but without success since they need to be ran with su . 我试图将其传递给busybox或toolbox,但是没有成功,因为它们需要与su一起运行。

Thank you! 谢谢!

It is possible , by having the same thread that makes the call to root commands stay and let it always be the only one that handles them. 通过具有使对根命令的调用保持不变的线程并使它始终成为唯一处理它们的线程,这是可能的。

This way, the toast will only appear the first time you use root operations. 这样,吐司只会在您第一次使用root操作时出现。

Also, on the end user side, some apps (like super-su) allow to avoid the toast, even per app. 另外,在最终用户方面,某些应用程序(例如super su)允许避免吐司,即使是每个应用程序也是如此。

Ok first to answer your question the answer is yes and no. 好吧,首先回答您的问题,答案是肯定的。

Easy Answer: No its not possible using one of the current SU managers like SuperUser or SuperSU you cant. 简便答案:使用当前无法使用的SU管理器(如SuperUser或SuperSU)之一是不可能的。 The toast is a safety mechanism. 烤面包是一种安全机制。 Both apps features to remove the toast for specific apps, but you as a dev can not control this. 这两个应用程序都具有删除特定应用程序的功能,但是您作为开发人员无法控制。

Hard Answer: Yes it is possible, but it would require you compiling your own su binary and using it in place of the su binary already installed. 艰难的回答:是的,这是可能的,但是它需要您编译自己的su二进制文件并使用它代替已安装的su二进制文件。 You would need to remove the code that references the current manager (Which ever source you compiled from). 您将需要删除引用当前管理器的代码(您从中编译的源代码)。 Would be recommend to add checks so that ONLY your app can run that binary. 建议添加检查,以便仅您的应用程序可以运行该二进制文件。 This can lead to security risks though and is probably is not a good idea. 但是,这可能会导致安全风险,并且可能不是一个好主意。

I did not look through your code very much but i would say one thing do NOT under any circumstance run SU commands on the UI thread. 我没有仔细检查您的代码,但是我想说一件事,无论如何在UI线程上都不能运行SU命令。 This is only asking for problems. 这只是在问问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM