首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行SU命令

执行SU命令
EN

Stack Overflow用户
提问于 2018-05-18 09:18:51
回答 1查看 529关注 0票数 0

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

代码语言:javascript
复制
 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:

代码语言:javascript
复制
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上设置了一个侦听器,然后它对一个“沉浸类对象”进行Intanciate,其中我应用了"setImmersiveFull()方法“

当我在我的真实手机上运行这个应用程序时,所有事情都进行得很顺利,即使我点击按钮满了,吐司也会出现,但屏幕一点也不改变,这很奇怪,因为当我在终端模拟器应用程序上运行命令时,屏幕会变成沉浸式模式。

以下是沉浸式课程:

代码语言:javascript
复制
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();
}




}

我的设备是根根的,它运行AndroidOreo8.0.0,我应该怎么做才能以SU编程的方式运行这个命令呢?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-18 10:32:46

您应该调用Shell.SU.run("command")而不是使用Runtime创建一个进程

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50407904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档