Search code examples
androidadbroot

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


I had rooted my device which is SAMSUNG GALAXY Note 4.

After that, I had installed a shell app from the app store and typed "su" and a Toast appeared stating that the shell guaranteed root access.

I had typed "setprop service.adb.tcp.port 5555" which gives command failed. So I typed "su - c 'setprop service.adb.tcp.port 5555' " which does not give error but also does not have effect since I still unable to connect wirelessly.

I had tried to run a non defined command like "su hdhdhd" which did not give an error!!!

But when opening adb shell from my PC when phone is connected by USB, I can successfully run su commands!!!

Why su commands can be executed from PC adb shell while on a phone shell it does not succeed????!!!!!!

I want to connect my phone with the pc through adb without using USB at all even at the first time...

Thanks in advance...


Solution

  • I had solved the problem by writing my own android app that runs a shell script containing the commands I want.

    The problem was that not all terminal emulators available on the play store function correctly especially with root privileges.

           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) {}
    

    I want this shell script to run automatically on system startup. There are several apps on the play store for this purpose, but they are malfunctioning! So, I had updated my app to run automatically on system startup by implementing a 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 Configuration:

    <?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>