yashsingh1/xdaforums huggingface dataset:
[Java for Android App Development] Deleted member 4303594:
You can learn here how to execute shell commands as root and read output and errors

What you will need:

Eclipse with ADT plugin

Basic knowledge of java

Rooted android device

Note

Root commands should always be executed in background thread, you can use AsyncTask for example

I won't explain here how to use AsyncTask, maybe in another tut.

Also note that I'm a relative beginner myself so I won't use professional terms I'll try to explain in my own words, so I'm sorry in advance if you have no idea what I'm talking about :p

1. First thing that we need to do is open a new root shell like this:

Process process = Runtime.getRuntime().exec("su");

Make sure to destroy this process after finished

2. Open input output and error streams to write commands and read output

OutputStream stdin = process.getOutputStream();

stdin is used to write commands to shell. This is OutputStream, which means that using this stream we can execute command(like writing command in terminal)

InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();

stderr and stdout are used to read output and error of a command which we executed.

3. Now we actually execute commands

stdin.write(("ls\n").getBytes());
//after you exec everything that you want exit shell
stdin.write("exit\n".getBytes());

"\n" at the end of the command means new line(like when you press enter in terminal). This is important, if you dont add new line it same like you didn't press enter

4. Flush and close OutputStream

stdin.flush(); //flush stream
stdin.close(); //close stream

5. Read output and error of a executed command

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();

We read output and error (if any) line by line and write it to logcat

You can of course do anything with output(display in TextView for example)

6. Finally we destroy opened shell

process.waitFor();//wait for process to finish
process.destroy();

You need to handle InteruptedException and IOException.

Hope this helps someone. Again sorry for stupid explanations. I totally understand all this but English isn't my primary language so its a but hard to explain...

Here is whole code;

try {
String line;
Process process = Runtime.getRuntime().exec("su");
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();

stdin.write(("ls\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 ex) {
}
[Java for Android App Development] Deleted member 4303594:
Yea roottools is better solution, it handles opening shell for you, its easier, less code, and in my experience a little bit faster.

Here is an example:

Command command = new Command(0, "ls")
{
@Override
public void output(int id, String line)
{
// Handle output here
}
};
RootTools.getShell(true).add(command).waitForFinish();

And also do this when exiting application

RootTools.closeAllShells();
[Java for Android App Development] ramdroid77:
Everyone should read the How-To SU guide by Chainfire:

http://su.chainfire.eu/

Usable example code is on Github. In the meanwhile there's an interactive shell (like in RootTools) available too:

https://github.com/Chainfire/libsuperuser
[Java for Android App Development] octobclrnts:
I noticed that you called your InputStream stdout and your OutputStream stdin. Is there any reason that you chose to reverse the usual naming?
[Java for Android App Development] sak-venom1997:
Great work but i would be delighted if op mentioned root commands and how to use them
[Java for Android App Development] Deleted member 4303594:
Its confusing I know.

I'll try to explain

You use InputStream to read output of the shell so I called it stdout

Output of a shell/terminal is called stdout

You use OutputStream to write to shell(input to shell) so its stdin

Passing commands to terminal is stdin

It stands for standard output/input

More about stdin, stdout, stderr

http://en.m.wikipedia.org/wiki/Standard_streams
[Java for Android App Development] Deleted member 4303594:
There is no such thing as root command.

commands can be executed as root user or as normal user.
[Java for Android App Development] sak-venom1997:
You didn't get me sir I ment the commands which run as root and how can developers utilize them

Hit Thanx Button if i helped you!
[Java for Android App Development] Deleted member 4303594:
I'm not really sure what you are asking. Any command can be executed as root.

Maybe you should read a bit about linux and shell
[Java for Android App Development] sak-venom1997:
No I was talking about the commands which require root to run like ifconfig

Sry for trouble I have no linux knowledge

Hit Thanx Button if i helped you!
[Java for Android App Development] nikwen:
There are some commands that will just make sense as root. However, why should anyone write a tutorial about how to use some commands very few persons will need. Google "Linux command <what you want to do>" and you will find explanations. Many commands are just more flexible when executed like this.

I really recommend that. You will need it when you develop a root app. And you can use the adb shell! Great help.

@OP: What's about mentioning that you should use the busybox commands as the system's implementation of the shell commands differs from device to device and from ROM to ROM?

I also recommend RootTools. One of the best libraries in my opinion!
[Java for Android App Development] Deleted member 4303594:
Purpose of this tutorial is to show how to execute commands as root, not how to use certain Linux commands.

And besides, using busybox is not always best solution, what if device doesn't have it installed, what if busybox doesn't have that command.

For example you would definitely not use "busybox echo" or "busybox ls".

Devs should already know how to use Linux, this is just to show how to do it from java.
[Java for Android App Development] nikwen:
You are right. It is true that nobody would use busybox for very simple commands.

However, RootTools has the RootTools.offerBusyBox(Activity activity) Method which opens Google Play to download a busybox installer.

I understood what you wanted to do.

Great job, btw. Would have been glad if I had had this when I started with root apps. ;)
[Java for Android App Development] RavinduSha:
Great Work!!!

I found how to execute root commands before. But this post has the best explanation. Thanks a lot!
[Java for Android App Development] ramdroid77:
I once did run into troubles parsing the results of "ls" command. Usually 'ls' is just the short table-style list, while you could get all the details with 'ls -l'. This is what I needed. But when testing on the Motorola Milestone unfortunately 'ls' was sym-linked to 'ls -l', therefore calling 'ls -l' would result in an error message. Don't know if more devices act like that (didn't test on any other Motorola phones, and the Milestone is quite old by now), but maybe it still makes sense to use busybox for 'normal' command in some cases...
[Java for Android App Development] abcdjdj:
Hello,

I am trying to run a script kept in my assests folder of my app. It is Root.sh which contains -

su
cd system
mkdir abcdjdj

This is my java code:-

String path = "file:///android_asset/Root.sh";
Process p = new ProcessBuilder().command(path).start();

But now I get a runtime error -

04-22 15:08:03.144: E/AndroidRuntime(785): Caused by: java.io.IOException: Error running exec(). Command: [file:///android_asset/Root.sh] Working Directory: null Environment: [ANDROID_SOCKET_zygote=9, ANDROID_STORAGE=/storage, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/mnt/sdcard, ANDROID_ASSETS=/system/app, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ASEC_MOUNTPOINT=/mnt/asec, LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar, ANDROID_DATA=/data, LD_LIBRARY_PATH=/vendor/lib:/system/lib, ANDROID_ROOT=/system, ANDROID_PROPERTY_WORKSPACE=8,32768]

Can anyone please help me?

Thanks.
[Java for Android App Development] nikwen:
Why don't you do it this way?

Runtime.getRuntime().exec("su", "-c", "cd system; mkdir abcdjdj");

Note that you need to pass the commands you want to execute to the command method, not the path.
[Java for Android App Development] abcdjdj:
It gives a syntax error. I guess it should have been - Runtime.getRuntime().exec(new String[] { "su", "-c", "cd system; mkdir abcdjdj" });

It runs fine on my phone but I still don't see a folder called abcdjdj is /system
[Java for Android App Development] nikwen:
You are right, it should have been that.

Try to add the full path:

Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(new String[] {"su", "-c", "mkdir /system/abcdef"});
runtime.exec(new String[] {"su", "-c", "mkdir /system/aaab; mkdir /system/aaac"});
} catch (IOException e) {
e.printStackTrace();
}

I think that executing your first idea should work if you execute

runtime.exec("su");

and then write the commands to the outputstream as described in the first post of this thread.

---------- Post added at 06:13 PM ---------- Previous post was at 06:06 PM ----------

However, I recommend using roottools.

If you need to execute commands rarely it will be fine that way, but if you need to execute commands often, there will be annoying Toast messages every time. Using roottools, there will be such a message just once when the app requests SU rigths for the first time after the launch.
[Java for Android App Development] nikwen:
For a very basic tutorial, check this: http://code.google.com/p/roottools/wiki/Usage
[Java for Android App Development] nikwen:
What is the advantage of CMD processor?

Btw, you are one of the best programmers I have ever seen assisting others with programming apps on xda.
[Java for Android App Development] Androguide.fr:
Well I'd say the advantage is that it's lighter than the roottools jar and the syntax to call it is more concise (no try/catch needed). But I guess roottools has more features, idk, didn't use it very much

Thanks a lot for the compliment, I could say the same about you ;)

I try to help when I can but I really don't have much experience, for example I wouldn't have been able to answer the animation question you answered.

I started java & android apps about 6 months ago but I'm lucky enough to work from home so I have a lot of time to practice & learn everyday.

I love this new app development forum and I hope it will be more active soon (and that we'll get syntax highlighting eventually!)
[Java for Android App Development] nikwen:
Thank you, too. ;)
[Java for Android App Development] abcdjdj:
Roottools did the job easily.:)

---------- Post added at 12:21 PM ---------- Previous post was at 11:45 AM ----------

But now I get one more problem. This is my function:-

public void Check(View v)throws IOException
{
TextView a = (TextView)findViewById(R.id.textView1);
String path="/system"; String ans="";
FileOutputStream x;
FileInputStream y;int i;
if(RootTools.isRootAvailable())
{
if(RootTools.isAccessGiven())
{
x = new FileOutputStream(path+"/abcdjdj.txt");
x.write('D');
x.write('J');
//NOW READING
y= new FileInputStream(path+"/abcdjdj.txt");
do
{
i=y.read();
if(i==-1)
{

}
else
{
ans=ans+(char)i;
}
}while(i!=-1);
if(ans.equals("DJ"))
{
a.setText("ROOTED");
}
}
else
{
a.setText("ROOT PERMISSION NOT GRANTED");
}
}
else
{
a.setText("NOT ROOTED");
}
}

Now, after gaining root access I still can't write the abcdjdj.txt file to /system. It gives a run time error - EROFS (Read only Files system). Anyway to write files on /system partition?

EDIT - Thank you for your help guys. I got the problem. Now it's working:D
[Java for Android App Development] nikwen:
Did you need to mount it as rw?

Maybe it would be better to unmount it afterwards so that other apps cannot access the system partition.
[Java for Android App Development] abcdjdj:
I had not done that. Now I did and it is working absolutely perfect.:D
[Java for Android App Development] nikwen:
Great. :good:
[Java for Android App Development] DHD22800:
Hi

I know that this is an old thread but I can't get output...

In My CMDProcessor I declared a variable:

static String rit;

this is my method:

public static String runCustomCommandStdOut(String...commands) {
Command command = new Command(0, commands)
{

@Override
public void commandCompleted(int arg0, int arg1) {
// TODO Auto-generated method stub
Integer arg1I = arg1;
Log.d(Constants.TAG, "commandCompleted()" + arg1I.toString());
rit = arg1I.toString();
}

@Override
public void commandOutput(int arg0, String arg1) {
// TODO Auto-generated method stub
Log.d(Constants.TAG, "commandOutput(): " + arg1);
rit = arg1;
return;
}

@Override
public void commandTerminated(int arg0, String arg1) {
// TODO Auto-generated method stub
Log.d(Constants.TAG, "commandTerminated(): " + arg1);
rit = arg1;
}
};
try {
RootTools.getShell(true).add(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RootDeniedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(Constants.TAG, "Command: " + rit);
return rit;
}

rit is null, but with the log I can chech that the command output is correct.

How I can make a method ?

Thanks
[Java for Android App Development] siju2012:
ls -Fl or ls -Fhl doesn't work? It works though with adb or Terminal Emulator app. Any way to the long listing together with indication which file is directory,file or symlink?
[AT&T Samsung Galaxy Note II] marklumabi:
Need help please.

I downloaded all the required files as stated in the instruction and successfully flash the 4.1.1 / Baseband: Version VLALJ2 into my phone. The odin says it pass.

but my phone just hang up in samsung logo.waited for about an hour and still not booting.

Version before doing the flashing is:

Android version: 4.1.2 / Baseband: Version VLBMA3 and

im in SGH I317m.

any help please?

-----------

Good thing i was able to go to recovery mode and just factory reset. my phone rebooted normally.
[HTC Sensation] padre629:
Hello developers!!

HTC SENSATION 4G / VIPERS ROM 1.6.3 / SULTAN KERNEL

I've installed the Vipers 1.6.3 ROM, and it's more than excellent, really I saw the future (everyday,,,)

But I've a little question.

I come from Stock Rom with Sense 3.6, I've adapted fine to the keyboard distribution.

But in this new Rom (sense 4.1) I don't like the keyboard!!

I feel that the keys are very small hahahahahaah..... and other things.

I wonder if is possible to get the Sense 3.6 keyboard in Sense 4.1

Thanks for your help!!!
[HTC Sensation] ashkan-khatar:
Well, tht's not a problem, can't you use MultiLing keyboard for example? Or SwiftKey.

But about the sense 3.6 keyboard, actually i do like it too, but I've seen only vice versa while searching google :D don't know if it's possible

That pretty "Thanks" button's waiting for U, in case I helped :P

∫εทт ƒя๑๓ α ∫нαкεท บρ нтς ∫εทรαтï๑ท жξ บรïทg... พнαтενεя! Dïƒƒεяร?!
[HTC Sensation] padre629:
So, I've installed MultiLing keyboard..... It's perfect (almost)...

This app has many options, many forms to configurate it.... I'll download a Spanish dictionary!!!

Thank you very much!!
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] Tyler.Boston1:
Hey im trying to flash P.A.C-Man rom from Aocp the collective and twrp 2.4.4.0 is telling me failed over and over, but if i try to flash Aocp again it does it no problem. What's wrong with it?
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] xBeerdroiDx:
what exactly is the error message?

try going back to an earlier version of twrp

here's 2.4.3.0

flash in twrp, reboot recovery, flash rom. report back
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] Tyler.Boston1:
And i tried the latest CWM touch and it said installation aborted.
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] xBeerdroiDx:
might be a bad download. grab it again and try flashing
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] Tyler.Boston1:
Yeah it was a bad one, I went over to the forum and saw others complaining about it so i flashed liquid smooth instead. Thanks though!
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] xBeerdroiDx:
i think you'll be happier on liquid anyways. cheers
[AT&T, Rogers, Bell, Telus Samsung Galaxy S III] y0yerrj0sh:
The Op said it was a basketbuild error (the site that hosts the rom). I found the the 4/5 nightly failed to install in TWRP for me also but the 4/3 worked fine and i think the issues have been fixed so the next nightly should work fine.
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] dano5595:
Can i run CM10 on my SGY?
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] hell_lock:
Cm10 is old. Go for cm11
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] sanny5:
No dude u can't

Wat do u do here on xda? :rolleyes:

I see ur posts and threads everywhere yet u made a thread for this simple question? ?

=========HOLO-UBUNTU========theme
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] alexche77:
CM 7.5 has bugs (bluetooth i think) and CM 10 has even more bugs..(wifi, and many others) so yet there are no drivers for it.
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] Sachin Patel:
CM 7 and CM9 are not fully working for galaxy y then CM 10...

how it work??

press thanks if i help you :)

sent from my GT-S5360 using

XDA PREMIUM
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] DEVILKOIRALA:
i wonder why search button is made??:confused::confused:
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] sohan rawat:
yess 10.1 wrking very well in our y :eek: and there is latest version comming in this may android 5.0 . jst w8 for 1 mnth. then update sgy to 5.0 :laugh: :laugh: :laugh:

sorry dude.. search before posting on xda. there is no stable version of cm. even cm7 not fully wrking yet. but our devs wrking on cm7 to make it fully stable. if u cnt contribute in making cm stable then jst w8 and watch :good: :good:
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] deathnotice01:
Can it run cm10? Yes!!!!

Can we? No!!!
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] junior061197:
Not even this man cm7 or 9 yet. Maybe soon maybe. Greetings. ;)
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] Aldich13:
LOL!! , i think it's didn't work. Coz on sgy haven't a driver for JB. :D
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] optimusinc:
No dude u cant run cm10 but u can go for cm9 though it is unstable.And cm7 is fully working for SGY
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] Sachin Patel:
cm7 fully working??

press thanks if i help you :)

sent from my GT-S5360 using

XDA PREMIUM
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] TheDeadEnd:
BRO I think ur nuts!

Sent from my Huawei Glory Honor U8860 Using CM10.1
[Galaxy Y GT-S5360 and Duos 6102 Q&A, Help & Troubl] Zius:
try to reflash your brain. CM10 :eek:
[MDA II, XDA II, 2060 ROM Development] jazlee:
I am a new convert to XDA II world. I had Nokia 3650 but traded for this awsome toy. But I do miss one feature from XDA II. I have this program where it will know where I am at a particular time because it looks at the cell block. SO I could program it to change to certain profile when I am at movie, it will turn off the ring and when I came out of the movie the ring will come back on. Can anyone tell me if this kind of of program exist for XDA? also if it is possible for some to code it?

thanks
[MDA II, XDA II, 2060 ROM Development] itsme:
t-mobile has some services based on cell-id, called t-zones or home-zone.

but I found that the xda-ii becomes quite unstable if you enable reception of the cellid, the phone part crashes every 1-2 hours orso. and sometimes stays dead, until you reboot.

see tstril2.cpp and RilClass.cpp for how to get the cellid.
[General Questions and Answers] emregezgin:
Hey xda..

Im looking for something that can make my laptop work as a headset for my android phone..

I need to be able to use the speakers and the microphone on the laptop as source when phone is connected to the laptop over bluetooth..

My phone is a Samsung Galaxy Note 2

And right know am i using a broadcom bluetooth device, but if i need something els just tell me then i will change it to make it work..

Im right now using Remote Phone Call app and its working fine.. i can answer and make calls from my pc

https://play.google.com/store/apps/details?id=justPhone.remotePhone

but i have an extra unnessessry bluetooth unit in my car that i want to get rid of so i can connect my carpc digitally to my amplifiers insted of the way its done now..

Does anybody know anything about this? Tried stuff like this??

Please help :)
[Verizon Samsung Galaxy S III] hyelton:
So I went to Verizon in December and got an account for my Mum and Dad got them both S3`s (They had U.S.Cellular) But lately its been horrible.

Well anyway Got them 2 brand new phones. My Dad`s S3 today still works Perfectly Fine! But my Mum`s is having bad luck.

1st phone which was new - Got VERY hot with no use even after reset and would get so hot it would turn off. Called Verizon

2nd phone Referb - Signal Issues from Day 1 constantly going in and out

3rd phone Referb - Text Messages would NOT go out, Incoming everything worked! But Placing a Call or sending a Text would NOT work at all.

4th phone Referb - Just got it today So far so good but so far so good with the others. What should I do if this one starts experiencing it?

Anyone had this bad of luck?
[Verizon Samsung Galaxy S III] mistyv:
My first phone started doing what her 3rd one was doing after 2 weeks of having it, then my second died after the first time I charged it. So far the 3rd has held up for 6 and a half months.
[Verizon Samsung Galaxy S III] Addiso:
If you still continue to experience issues you can always ask Verizon for a different phone explaining that you've actually had it with this phone replacing it and such.
[Verizon Samsung Galaxy S III] hyelton:
That`s what Im going to do if something happens this time.
[Sprint HTC EVO 4G LTE] mstrblueskys:
So... I'm thinking that before I get my One I want to test the Facebook Home so I'm not feeling the urge to see what it is and spoil my One experience.

What I'm looking for is an ASOP with group MMS. It's on the Sense 5 messaging app and I am pretty attached to it now.

Also, if you want to recommend features from a specific ASOP build, I would appreciate it. I've only done Sense builds up until this.

Thank you!