簡體   English   中英

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

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

我正在開發一個簡單的應用,該應用通過執行shell命令在build.prop上插入行。 我的主要問題是,每次檢查創建該功能的切換開關時,都會出現一個顯示外殼字符串的吐司。 有什么辦法可以避免這種情況? 此外,如果您有任何建議要清理一點,代碼將不勝感激! (對我來說第一個應用程序)。

碼:

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

因此,我的主要問題是su -c表示令人討厭的吐司。 我試圖將其傳遞給busybox或toolbox,但是沒有成功,因為它們需要與su一起運行。

謝謝!

通過具有使對根命令的調用保持不變的線程並使它始終成為唯一處理它們的線程,這是可能的。

這樣,吐司只會在您第一次使用root操作時出現。

另外,在最終用戶方面,某些應用程序(例如super su)允許避免吐司,即使是每個應用程序也是如此。

好吧,首先回答您的問題,答案是肯定的。

簡便答案:使用當前無法使用的SU管理器(如SuperUser或SuperSU)之一是不可能的。 烤面包是一種安全機制。 這兩個應用程序都具有刪除特定應用程序的功能,但是您作為開發人員無法控制。

艱難的回答:是的,這是可能的,但是它需要您編譯自己的su二進制文件並使用它代替已安裝的su二進制文件。 您將需要刪除引用當前管理器的代碼(您從中編譯的源代碼)。 建議添加檢查,以便僅您的應用程序可以運行該二進制文件。 但是,這可能會導致安全風險,並且可能不是一個好主意。

我沒有仔細檢查您的代碼,但是我想說一件事,無論如何在UI線程上都不能運行SU命令。 這只是在問問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM