执行SU命令

时间:2018-05-18 09:18:52

标签: android shell superuser

我正在开发一个可以在Root设备上设置沉浸式模式的应用程序,但是我无法执行命令,在主要活动上我检查设备是否已植根,如果是,我开始意图打开第二个活动,我有一个需要执行命令的按钮:

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;

 import eu.chainfire.libsuperuser.Shell;

 public class MainActivity extends AppCompatActivity {

     Button btnSU;


     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         btnSU = findViewById(R.id.bv_checkSu);

         btnSU.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                  checkSU();
             }
         });




     }

     private void checkSU(){

           try {

                 if (Shell.SU.available()) {


                Toast.makeText(this, "Congratulation you're SU", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(this, setScreenActivity.class);
                startActivity(i);
                finish();


            }else{

                Toast.makeText(this,"Sorry No SU shell Found",Toast.LENGTH_SHORT);
            }

    }catch(Exception e){

        e.printStackTrace();
        Toast.makeText(this,"Sorry an Error has Occured",Toast.LENGTH_SHORT).show();
    }

}


}

setScreenActivity:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class setScreenActivity extends AppCompatActivity {

Button btnFull;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    btnFull = findViewById(R.id.bv_Full);

    btnFull.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {

                Immersive im = new Immersive();
                im.setImmersiveFull();

            }catch(IOException e){

                e.printStackTrace();
            }
        }
    });





}
}

在setScreenActivity上我在Button上设置了一个监听器,然后它使用了一个"沉浸式的类对象,我在其中应用" setImmersiveFull()方法"

当我在我的真实手机上运行应用程序时,即使我点击按钮已经完成,即使屏幕显示但屏幕根本没有变化,所有事情都进展顺利,这非常好奇,因为当我在终端仿真器应用程序上运行命令,屏幕变为沉浸式模式。

这里是沉浸式课程:

import android.content.Context;
import android.widget.Toast;
import java.io.IOException;


public class Immersive {

public Context ctx;

public void setImmersiveFull() throws IOException {

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(String[] {"settings put global policy_control immersive.full=apps,-com.google.android.googlequicksearchbox"});

    Toast.makeText(ctx,"Full Immersive Mode Set",Toast.LENGTH_SHORT).show();

    }

public void setImmersiveNoNavBar() throws IOException{

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(String[] {"settings put global policy_control immersive.navigation=apps,-com.google.android.googlequicksearchbox"});

    Toast.makeText(ctx,"No Navigation Bar Immersive Mode Set",Toast.LENGTH_SHORT).show();

}

public void setImmersiveNoStatusBar() throws IOException{


    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(String[] {"settings put global policy_control immersive.status=apps,-com.google.android.googlequicksearchbox"});

    Toast.makeText(ctx,"No Status Bar Immersive Mode Set",Toast.LENGTH_SHORT).show();

}

public void setStockMode() throws IOException{


    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(String[] {"settings put global policy_control immersive="});

    Toast.makeText(ctx,"Stock Mode Set",Toast.LENGTH_SHORT).show();
}




}

我的设备已植根,它运行Android Oreo 8.0.0 任何想法我应该以编程方式运行此命令吗?

由于

1 个答案:

答案 0 :(得分:0)

您应拨打Shell.SU.run("command")而不是使用Runtime

创建流程