"su" 在 Android 既不报错也不生效。如何 运行 su 命令在 root 设备上成功?

"su" on Android neither giving error nor taking effects. How to run su commands successfully on rooted devices?

我的设备是 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",但没有给出错误!!!

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

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

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

提前致谢...

我通过编写 自己的 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>