How to set a system property from an android app?

TheMonkeyKing :

I need to do setprop but from an app.

Right now I'm trying to just run it as a shell command:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "test:MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exec("setprop service.adb.tcp.port 5555");
    }

    private void exec(String cmd) {
        Process proc = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;

        Runtime runtime = Runtime.getRuntime();
        Log.d(TAG, "exec command: " + cmd);
        try {
            proc = runtime.exec(cmd);
            successResult = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String s;
            while ((s = successResult.readLine()) != null) {
                Log.d(TAG, "exec command: " + s);
            }
            while ((s = errorResult.readLine()) != null) {
                Log.d(TAG, "exec command: " + s);
            }
        } catch (IOException e) {
            Log.e(TAG, "Error running shell command:", e);
        } finally {
            try {
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                Log.e(TAG, "Error running shell command:", e);
            }

            if (proc != null) {
                proc.destroy();
            }
        }
    }
}

This give me:

exec command: setprop service.adb.tcp.port 5555

exec command: setprop: failed to set property 'service.adb.tcp.port' to '5555'

How can I set a system property from an application?

Edit: Sorry, I should have I added my device is rooted and this app is a system app.

Lev M. :

You can't set this particular property from a regular app. It is protected by the system.

Before Android 5, you need your app to have sharedUserId="android.uid.system" in your manifest, and from Android 5 and up it needs to be sharedUserId="android.uid.shell".

However, if your app has one of these user IDs, it must be signed with the system certificate to be installed, so you can only do this on development boards and custom ROMs.

Another option is if your device is rooted, you can run the setprop command with su.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=92468&siteId=1