0

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

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

}


}

设置屏幕活动:

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() 方法”的“沉浸式类对象”

当我在我的真实手机上运行该应用程序时,即使我完全单击按钮,一切都非常顺利,吐司出现但屏幕根本没有改变,这很奇怪,因为当我在终端模拟器应用程序上运行命令时,画面变为沉浸式模式。

这是沉浸式课程:

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 任何想法我应该如何以 SU 编程方式运行此命令?

谢谢

4

1 回答 1

0

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

于 2018-05-18T10:32:46.643 回答