android - 执行 SU 命令
问题描述
我正在开发一个可以在 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 编程方式运行此命令?
谢谢
解决方案
您应该调用Shell.SU.run("command")
而不是使用创建进程Runtime
推荐阅读
- api-design - 针对不同格式的一种资源的 Restful API 命名约定
- javascript - 我怎样才能得到这个函数来检查一个空字段?
- ansible - 如何从ansible中的嵌套列表中获取项目列表
- javascript - 如何向本地主机发送 axios 请求?
- excel - 使用 VBA 过滤 OLAP 数据透视表
- c# - 从 blazor 页面传递 UserId
- java - DFS 方法没有产生预期的输出
- javascript - 如何在 Material UI 中默认选中第一个单选按钮?
- google-app-engine - 无法在 Google AppEngine 柔性环境上部署 websocket
- sql - 从 postgresql 中的表推断行类型