3

I want to use commandline to start an android intent (say play a video in the Gallery app). Can I start the intent as root?

I tried

su -c "am start -a android.intent.action.VIEW -n com.android.gallery3d/.app.MovieActivity -d <some_file>"

It throws the error

su: exec failed for am start -a android.intent.action.VIEW -n com.android.gallery3d/.app.MovieActivity -d <some_file> Error:No such file or directory

EDIT :

This works alright (plays a video in gallery without root permissions). I want to run gallery with root permissions though.

am start -a android.intent.action.VIEW -n com.android.gallery3d/.app.MovieActivity -d <some_file>
CtrlF
  • 43
  • 1
  • 4
  • Take a look at these links http://stackoverflow.com/questions/3512198/need-command-line-to-start-web-browser-using-adb , http://stackoverflow.com/questions/6613889/how-to-start-an-android-application-from-the-command-line – Ravi Aug 28 '13 at 05:20
  • Thanks Ravi, but this isn't what I am looking for. The am command works normally without su. I'm looking to start the am command as root. – CtrlF Aug 28 '13 at 07:00

2 Answers2

2

It seems that there are several issues regarding to your question.

First, what is your real requirement?

1. Send an intent as root (root sends the intent)
2. Start an activity as root (gallery runs as root)

Regarding to the issues you have met.

1.send and intent as root is fairly easy as you also tried. You can su to be root and then use 'am start xxx' should work. I guess the reason why your 'su -c "am start xxx"' failed is due to env issues. You can try to do something like:

mProcess = new ProcessBuilder()
                    .command("/system/xbin/su")
                    .redirectErrorStream(true).start();
OutputStream out = mProcess.getOutputStream();
String cmd = "am start am start -a android.intent.action.VIEW "
                "-n com.android.gallery3d/.app.MovieActivity "
                "-d <some_file> \n";
out.write(cmd.getBytes());

2.run gallery as root user won't work, gallery app wont be running as root. It is the system limitation unless you modify the framework (the zygote process).

And I guess your target is to play some video which is not accessible for a normal application like gallery? You can try other approaches like temporarily change the permission of the file other than trying to run gallery as root.

Robin
  • 10,052
  • 6
  • 31
  • 52
2

Simple answer is no, you cannot start any Android apps or intents as root, even if you launch them from terminal as root. This is due to the way the Android system is designed: even if you use su to launch an app, the process that actually starts the app forks the app into an unprivileged thread. For most purposes, the su binary is meant to be launched programmatically from within an app that has already been started with non-root access; otherwise, the su binary is used to run native Linux programs / commands, not Android apps.

The only way to get Android apps to natively have 'root' access is to install a kernel that grants root access to absolutely every process automatically (I have seen some cheap no-name brand devices do this right out of the box). This is generally a very bad idea, because it becomes extremely easy to accidentally destroy the device.

The real question is why you want to do this in the first place. If you really need to have the gallery access a privileged folder, consider installing busybox and mounting the protected directory as a public directory somewhere. This will require that you have a rooted device.

aggregate1166877
  • 2,196
  • 23
  • 38