【问题标题】:"su" on Android neither giving error nor taking effects. How to run su commands successfully on rooted devices?Android 上的“su”既不报错也不生效。如何在 root 设备上成功运行 su 命令?
【发布时间】:2019-11-27 18:48:22
【问题描述】:

我的设备是 SAMSUNG GALAXY Note 4。

之后,我从应用商店安装了一个 shell 应用并输入“su”,然后出现一个 Toast,说明 shell 保证 root 访问权限。

我输入了“setprop service.adb.tcp.port 5555”,导致命令失败。 所以我输入了“su - c 'setprop service.adb.tcp.port 5555'”,它没有给出错误但也没有效果,因为我仍然无法无线连接。

我曾尝试运行一个未定义的命令,例如“su hdhdhd”,但没有报错!!!

但是当手机通过USB连接时从我的PC打开adb shell时,我可以成功运行su命令!!!

为什么 su 命令可以从 PC adb shell 执行,而在手机 shell 上却不成功????!!!!!!

我想通过adb将我的手机与电脑连接起来,即使是第一次也不使用USB...

提前谢谢...

【问题讨论】:

    标签: android adb root


    【解决方案1】:

    我已经通过编写我的自己的 android 应用程序解决了这个问题,该应用程序运行一个包含我想要的命令的 shell 脚本

    问题在于,并非所有 Play 商店中可用的终端仿真器都能正常运行,尤其是在具有 root 权限的情况下。

           try {
            String line;
    
            Process process = Runtime.getRuntime().exec("su");
            OutputStream stdin = process.getOutputStream();
            InputStream stderr = process.getErrorStream();
            InputStream stdout = process.getInputStream();
    
            stdin.write(("setprop service.adb.tcp.port 5555\n").getBytes());
            stdin.write(("stop adbd\n").getBytes());
            stdin.write(("start adbd\n").getBytes());
            stdin.write("exit\n".getBytes());
            stdin.flush();
    
            stdin.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            while ((line = br.readLine()) != null) {
                Log.d("[Output]", line);
            }
            br.close();
            br =
                    new BufferedReader(new InputStreamReader(stderr));
            while ((line = br.readLine()) != null) {
                Log.e("Error", line);
            }
            br.close();
    
            process.waitFor();
            process.destroy();
    
        } catch (Exception exception) {}
    

    我希望这个 shell 脚本在系统启动时自动运行。 Play 商店中有几个用于此目的的应用程序,但它们都出现故障!因此,我通过实现 BroadcastReceiver 更新了我的应用程序以在系统启动时自动运行。

    package com.example.liveandroid;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class ADBD extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
                Intent i = new Intent(context, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        }
    }
    

    AndroidManifest 配置:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.liveandroid">
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            tools:ignore="GoogleAppIndexingWarning">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name=".ADBD">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2019-12-22
      相关资源
      最近更新 更多