Hi
I'm trying to get a reboot application working.
It should get root through "su" and then execute "reboot".
It does, but nothing happens (su request shows up)
I used several ways to execute the commands.
The ShellInterface classs from MarketEnabler and this snippet http://www.anddev.org/root_access_snippet-t8384.html
"su" then "reboot" -> nothing
"su -c reboot" -> nothing...
Always the same, it looks like it worked but the device is still up
adb logcat says nothing about.
What's wrong?
I'm running it directly on HTC Hero rooted of course
I've had this issue on several ROMs. I guess some just don't support the command.. *shrug*
It should work...
The reboot binary is present
Hello out there. Looks like you just have a tiny error with your code.
"su -c reboot" needs to look like so: "su -c 'reboot'" -- the actual command to be executed has to be in quotes.
That sould work without the quotes
Still can't find the problem.
I chowned and chmodded the reboot binary like the su binary still no change
how does adbd do it without root permission?
the adb daemon on the device has to be able to do it to be able to respond to 'adb reboot' on a non-rooted phone. Anybody know how it is able to perform the reboot?
Try this, i can confirm it works fine
Code:
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
{
os.writeBytes("reboot\n");
os.flush();
process.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Meltus said:
Try this, i can confirm it works fine
Code:
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
{
os.writeBytes("reboot\n");
os.flush();
process.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Click to expand...
Click to collapse
This requires a rooted phone. How does adbd reboot on a non rooted phone?
Meltus said:
Try this, i can confirm it works fine
Code:
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
{
os.writeBytes("reboot\n");
os.flush();
process.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Click to expand...
Click to collapse
It works on some, doesn't work on others. Seems to be something with how the phone is rooted, or how permissions are set, not exactly sure. Either way, just because it works for you doesn't mean it will work for everyone, however the code up there is probably a very good example for an implementation.
Some apps like ROM Manager have their reboot written using the NDK, so that may be a reason why they can reboot whereas other apps which try to reboot fail.
Meltus said:
Try this, i can confirm it works fine
Code:
process.waitFor();
Click to expand...
Click to collapse
asdfasdf
Hi, i am trying to run adb and su commands in onclicklistener of my button of android app and here is my code
Code:
btnCheckSu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Process p; String[] st = new String[3];
try {
st[0]="/system/bin/su";
st[1]="-c";
st[2]="reboot";
p = Runtime.getRuntime().exec(st);
Toast.makeText(SUCheck.this, "Rebooting...", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
I do get the prompt for su but then phone does not reboot...any help please
thanks
Well im not sure about the su commands but the reboot intent is restricted to processes signed with the same key as the platform you are using (essentially the key used to sign the rom you are using)
It could be the same with using su to reboot. Not sure though
--------
I tried it on the terminal emulator and it worked after entering the command twice for some reason
From something awesome
Try your commands via adb root shell first. If they work then u know the condemns are good and can try something else. All so without capturing process output there it's no way to know what's going on.
Sent from my MB860 using Tapatalk
Why not use the standard android api?
Code:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot("");
You'll need the REBOOT permission though.
HomerSp said:
Why not use the standard android api?
Code:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot("");
You'll need the REBOOT permission though.
Click to expand...
Click to collapse
Because as i said before the reboot permission is only valid on system apps signed with the platform key
From something awesome
killersnowman said:
Because as i said before the reboot permission is only valid on system apps signed with the platform key
From something awesome
Click to expand...
Click to collapse
You're right, sorry.
Try this instead, it worked fine for me.
Code:
Runtime.getRuntime().exec("su -c /system/bin/reboot");
i guys
thanks for the above replies...
but none of the above suggestion has worked for me...any other clues please
thanks
.../me repeats himself...
CAPTURE UR OUTPUT, without it we cant know what is going wrong.
Try your commands via adb shell first, if they work there they will also work via your phone.
yup, great.... adb working through my pc shell
capturing output...i have to work that out how...cheers
hisheeraz said:
yup, great.... adb working through my pc shell
capturing output...i have to work that out how...cheers
Click to expand...
Click to collapse
from your process create datastreams for your process.getInputStream() and process.getErrorStream(). After execution dump streams to file/logcat/w/e.
hisheeraz said:
yup, great.... adb working through my pc shell
capturing output...i have to work that out how...cheers
Click to expand...
Click to collapse
Try this:
Code:
btnCheckSu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Process p; String[] st = new String[3];
try {
st[0]="/system/bin/su";
st[1]="-c";
st[2]="reboot";
p = Runtime.getRuntime().exec(st);
[COLOR="Red"] String s;
DataInputStream is=new DataInputStream(p.getInputStream());
DataInputStream er=new DataInputStream(p.getErrorStream());
Log.i("MyApp","stdout:\n");
while((s=is.readLine())!=null){
Log.i("MyApp",s+"\n");
}
Log.i("MyApp","stderr:\n");
while((s=er.readLine())!=null){
Log.i("MyApp",s+"\n");
}
[/COLOR]
Toast.makeText(SUCheck.this, "Rebooting...", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
sorry for being such a pain but my Log.i is empty there is absolutely nothing there
even after reinstalling the app in my phone nothing is in Log.i
help please...
EDIT:1
HERE IS THE LOG.I
sdtout:
stderr:
not permitted!
also i am using REBOOT permission in my manifest...i donot think i need it but just in case
hisheeraz said:
sorry for being such a pain but my Log.i is empty there is absolutely nothing there
even after reinstalling the app in my phone nothing is in Log.i
help please...
EDIT:1
HERE IS THE LOG.I
sdtout:
stderr:
not permitted!
Click to expand...
Click to collapse
bla,
try without -c. then also try...
reboot
reboot -f
busybox reboot
busybox reboot -f
hey thanks
/system/bin/su
-c
busybox reboot -f
is working...but
busybox reboot recovery -f
of
busybox reboot recovery
is not working. thanks for your effort and help,
i will dig into this tomorrow and will post you my feed back, it is really late here in AU :O
regards
Hello everyone, I developed an app which utilizes database.
I implemented a method for backing up the database from /data/data/package_name/databases to the SDCARD wiith the following code
Code:
try {
File path_to_sd = Environment.getExternalStorageDirectory();
File path_to_data = Environment.getDataDirectory();
String current_db_path = "//data//" + getPackageName() + "//databases//";
String path_to_database = path_to_data.getAbsolutePath() + current_db_path;
String backup_name = "backup.db";
File current_db = new File(path_to_database, database_name);
File db_backup = new File(path_to_sd, backup_name);
FileChannel src = new FileInputStream(current_db).getChannel();
FileChannel dst = new FileOutputStream(db_backup).getChannel();
if(!db_backup.exists()){
try {
db_backup.createNewFile();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Exporting database done successfully!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
the thing is that the above code did the job but created a file with the following permissions ---rwxr-x
so my method for importing the database file from the SDCARD back to the /data/data/pachage_name/databases fail at the line
FileChannel src = new FileInputStream(current_db).getChannel();
and the error is that, no read permission for this file.
How can I fix this issue?
thanks a lot.
needed root
shakash3obd said:
Hello everyone, I developed an app which utilizes database.
I implemented a method for backing up the database from /data/data/package_name/databases to the SDCARD wiith the following code
Code:
try {
File path_to_sd = Environment.getExternalStorageDirectory();
File path_to_data = Environment.getDataDirectory();
String current_db_path = "//data//" + getPackageName() + "//databases//";
String path_to_database = path_to_data.getAbsolutePath() + current_db_path;
String backup_name = "backup.db";
File current_db = new File(path_to_database, database_name);
File db_backup = new File(path_to_sd, backup_name);
FileChannel src = new FileInputStream(current_db).getChannel();
FileChannel dst = new FileOutputStream(db_backup).getChannel();
if(!db_backup.exists()){
try {
db_backup.createNewFile();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Exporting database done successfully!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
the thing is that the above code did the job but created a file with the following permissions ---rwxr-x
so my method for importing the database file from the SDCARD back to the /data/data/pachage_name/databases fail at the line
FileChannel src = new FileInputStream(current_db).getChannel();
and the error is that, no read permission for this file.
How can I fix this issue?
thanks a lot.
Click to expand...
Click to collapse
since u r trying to copy files to /data/data which is only read only mode ... so u need root access to do your job.....
i have attached a a zip file that contains 4 java files and can be included in your project...
it gives you root access... but writing to /data/data must be implemented by u...
Thank you so much anurag.dev1512.
I will take a look at the files you provided.
I will not be able to get back to you about the result soon since it's almost finals time so I will be busy until December
again, thanks a lot.:good:
Hello there,
I know this is a noob question but I am unable to implement libsuperuser by chainfire for my app, I can't understand the Asyncactivity he creates in his example project. I have tried that way for a simple Root Checker app, but i am unable to do it, beacuse my app doesn't ask for superuser permissions
So, could anyone please point me to any sample program using his libs, which is simple enough for a beginner to understand .
Thanks in Advance.
RootTools
gh0stslayer said:
Hello there,
I know this is a noob question but I am unable to implement libsuperuser by chainfire for my app, I can't understand the Asyncactivity he creates in his example project. I have tried that way for a simple Root Checker app, but i am unable to do it, beacuse my app doesn't ask for superuser permissions
So, could anyone please point me to any sample program using his libs, which is simple enough for a beginner to understand .
Thanks in Advance.
Click to expand...
Click to collapse
Why don't you try roottools library? It's really simple, and intuitive. Lots of documentations also available!
Here is a link for you : http://code.google.com/p/roottools/
For su request on start, you must have:
Code:
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
in your manifest.
To check for su :
Code:
if(RootTools.isRootAvailable())
Log.e("YourTag","We have root!");
else Log.e("YourTag","No root access :( !");
nerroSS said:
Why don't you try roottools library? It's really simple, and intuitive. Lots of documentations also available!
Here is a link for you : http://code.google.com/p/roottools/
For su request on start, you must have:
Code:
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
in your manifest.
To check for su :
Code:
if(RootTools.isRootAvailable())
Log.e("YourTag","We have root!");
else Log.e("YourTag","No root access :( !");
Click to expand...
Click to collapse
Thanks for your reply. I tried adding the permission but it didn't work with libsuperuser. It must be something I wrote in the program.
I am now trying with roottools -
so if I have to copy a file from data/app to sdcard, how will I pass the shell command using RootTools ?
gh0stslayer said:
Thanks for your reply. I tried adding the permission but it didn't work with libsuperuser. It must be something I wrote in the program.
I am now trying with roottools -
so if I have to copy a file from data/app to sdcard, how will I pass the shell command using RootTools ?
Click to expand...
Click to collapse
Code:
CommandCapture command1 = new CommandCapture(0,"echo 1 > /sdcard/mycommandouput");
try {
RootTools.getShell(true).add(command1).waitForFinish();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Or for multi-command
Code:
CommandCapture command1 = new CommandCapture(0,"first command","second command here");
try {
RootTools.getShell(true).add(command1).waitForFinish();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Keep in mind that commands are as super-user, so no need to "su -c" / "su"..
nerroSS said:
Code:
CommandCapture command1 = new CommandCapture(0,"echo 1 > /sdcard/mycommandouput");
try {
RootTools.getShell(true).add(command1).waitForFinish();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Or for multi-command
Code:
CommandCapture command1 = new CommandCapture(0,"first command","second command here");
try {
RootTools.getShell(true).add(command1).waitForFinish();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Keep in mind that commands are as super-user, so no need to "su -c" / "su"..
Click to expand...
Click to collapse
How do I get past this error -
The method waitForFinish() is undefined for the type Command
Sorry for so many stupid questions, i am trying to learn
gh0stslayer said:
How do I get past this error -
The method waitForFinish() is undefined for the type Command
Sorry for so many stupid questions, i am trying to learn
Click to expand...
Click to collapse
I didn't close try{}, sorry! Put a "}" at the end of my code and it should work!
If you have any questions, feel free to pm me.
Edit 3: I have corrected my post. Copy paste the code above and should work now.
Here's a great RootTools tutorial: http://forum.xda-developers.com/showthread.php?t=2226664
nikwen said:
Here's a great RootTools tutorial: http://forum.xda-developers.com/showthread.php?t=2226664
Click to expand...
Click to collapse
nerroSS said:
I didn't close try{}, sorry! Put a "}" at the end of my code and it should work!
If you have any questions, feel free to pm me.
Edit 3: I have corrected my post. Copy paste the code above and should work now.
Click to expand...
Click to collapse
Thanks for your replies gentlemen. I finally settled for CMDProcessor and succeeded in making my command to work.
Now the command takes a long time to finish as it involves copying quite a few files and as it froze the main UI thread I used ASyncTask to take the heavy work to background thread.
Now I can't find a way to show a progress bar for the activities going on in the background thread. Can you please shed some light on how I could set some kind of progress update ... Thanks In Advance.
Edit : Should I use something other than ASyncActivity as the file copy opeartion takes several minutes and android developer website suggests to use executor or futuretask for tasks that require too long to complete
gh0stslayer said:
Thanks for your replies gentlemen. I finally settled for CMDProcessor and succeeded in making my command to work.
Now the command takes a long time to finish as it involves copying quite a few files and as it froze the main UI thread I used ASyncTask to take the heavy work to background thread.
Now I can't find a way to show a progress bar for the activities going on in the background thread. Can you please shed some light on how I could set some kind of progress update ... Thanks In Advance.
Edit : Should I use something other than ASyncActivity as the file copy opeartion takes several minutes and android developer website suggests to use executor or futuretask for tasks that require too long to complete
Click to expand...
Click to collapse
If it really takes that much time, I would use a remote service (a remote service runs in another process).
Using a remote service, closing the app using the recent apps menu won't stop that. The same applies for UI crashes. The copy operation won't stop if the UI crashes for whatever reason. You won't have to worry about orientation changes as well.
nikwen said:
If it really takes that much time, I would use a remote service (a remote service runs in another process).
Using a remote service, closing the app using the recent apps menu won't stop that. The same applies for UI crashes. The copy operation won't stop if the UI crashes for whatever reason. You won't have to worry about orientation changes as well.
Click to expand...
Click to collapse
Thanks for your reply again, I was trying to find a way to visualize the progress with a progress bar or something. Although I could not make a progress bar to work I was able to implement a spinner . . . Although a progressbar still would have been better, but for now, it serves the purpose of creating a sense of the activity happening in the background .
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
I will try to understand and implement the remote service that you linked, lets hope it isn't too much work .
Edit : The advantages you stated for remote service are all here too, even when I killed tha app from recents menu, the file copying operation still went on in the background .
Edit 2 : Orientation change only removes the spinner dialog, but the file copy operation proceeds until it's complete. The only thing that seems to stop the operation is when i uninstall the app from my device, while its still being executed. So I would say this baby is rock solid, just an addition of a progress bar would serve it well. :victory:
gh0stslayer said:
Thanks for your reply again, I was trying to find a way to visualize the progress with a progress bar or something. Although I could not make a progress bar to work I was able to implement a spinner . . . Although a progressbar still would have been better, but for now, it serves the purpose of creating a sense of the activity happening in the background .
I will try to understand and implement the remote service that you linked, lets hope it isn't too much work .
Edit : The advantages you stated for remote service are all here too, even when I killed tha app from recents menu, the file copying operation still went on in the background .
Click to expand...
Click to collapse
Well, you could try to do it like this:
Copy the files one by one or copy 5 at once. And afterwards set the progress to a higher value. That would, however, be more time consuming as the IPC (InterProcessCommunication) takes much time.
You could also do the copying using a script and "echo" some output after every 5 copied files. Then you would need to constantly listen for output and if you receive any, increase the progress value.
I would go with the spinner for performance though.
nikwen said:
Well, you could try to do it like this:
Copy the files one by one or copy 5 at once. And afterwards set the progress to a higher value. That would, however, be more time consuming as the IPC (InterProcessCommunication) takes much time.
You could also do the copying using a script and "echo" some output after every 5 copied files. Then you would need to constantly listen for output and if you receive any, increase the progress value.
I would go with the spinner for performance though.
Click to expand...
Click to collapse
Hmmm, interesting idea. I am using a shell command , which one would be better, guess I could echo some output to a dialog box.
On the contrary it would slow down the performance of app, so like you said spinner would be better for performance.
Still I would try to make some sort of progress bar work, please suggest which method should i use. I am using a shell command
"cp -r /data/app/ /sdcard/backup/apps" , to copy a whole directory to sdcard .
gh0stslayer said:
Hmmm, interesting idea. I am using a shell command , which one would be better, guess I could echo some output to a dialog box.
On the contrary it would slow down the performance of app, so like you said spinner would be better for performance.
Still I would try to make some sort of progress bar work, please suggest which method should i use. I am using a shell command
"cp -r /data/app/ /sdcard/backup/apps" , to copy a whole directory to sdcard .
Click to expand...
Click to collapse
This one would be slower but would give you an output after every copied file:
Code:
find /data/app -exec cp {} /sdcard/backup/apps \; -exec echo copy \;
It doesn't work properly with subdirectories though. However, that won't be important for you as /data/app doesn't contain subdirectories.
---------- Post added at 11:06 PM ---------- Previous post was at 10:58 PM ----------
Use this to get the total number of files in /data/app/:
Code:
ls /data/app/ | wc -l
---------- Post added at 11:09 PM ---------- Previous post was at 11:06 PM ----------
Using
Code:
ls -f /data/app | wc -l
will make it even faster because the list (whose lines are counted) won't be sorted.
nikwen said:
This one would be slower but would give you an output after every copied file:
Code:
find /data/app -exec cp {} /sdcard/backup/apps \; -exec echo copy \;
It doesn't work properly with subdirectories though. However, that won't be important for you as /data/app doesn't contain subdirectories.
---------- Post added at 11:06 PM ---------- Previous post was at 10:58 PM ----------
Use this to get the total number of files in /data/app/:
Code:
ls /data/app/ | wc -l
Click to expand...
Click to collapse
I was using this to get the no of files in the directory
find /data/app -type f -print| wc -l
But i couldn't figure out how to monitor the no of files copied to the sdcard to make a progressbar out of it.
And for this find /data/app -exec cp {} /sdcard/backup/apps \; -exec echo copy \ i assume the output will have to be shown in a dialog box.
On another note, thanks a lot for helping a nobody like me out. I am trying to learn app development and people like you are making it easier for me, means a lot to me. Keep up your awesome work and keep spreading your knowledge. :good:
gh0stslayer said:
I was using this to get the no of files in the directory
find /data/app -type f -print| wc -l
But i couldn't figure out how to monitor the no of files copied to the sdcard to make a progressbar out of it.
And for this find /data/app -exec cp {} /sdcard/backup/apps \; -exec echo copy \ i assume the output will have to be shown in a dialog box.
On another note, thanks a lot for helping a nobody like me out. I am trying to learn app development and people like you are making it easier for me, means a lot to me. Keep up your awesome work and keep spreading your knowledge. :good:
Click to expand...
Click to collapse
The new and faster version I posted above (ls -f /data/app | wc -l) should be the best (i.e. fastest) way to get the number of files.
Then you would need to constantly check the output of the java.lang.Process which runs your command. Change the progress value of the ProgressBar after every line of output.
I'll look into that tomorrow if you post a link to your CMDProcessor version in the meantime. Got to get some sleep now.
Thank you very much for the compliments... and welcome.
nikwen said:
The new and faster version I posted above (ls -f /data/app | wc -l) should be the best (i.e. fastest) way to get the number of files.
Then you would need to constantly check the output of the java.lang.Process which runs your command. Change the progress value of the ProgressBar after every line of output.
I'll look into that tomorrow if you post a link to your CMDProcessor version in the meantime. Got to get some sleep now.
Thank you very much for the compliments... and welcome.
Click to expand...
Click to collapse
Yeah man, I should get some sleep too. Been at this since 6 PM and its 4 AM now.
BTW here is the link for CMDProcessor I used
http://forum.xda-developers.com/showpost.php?p=40579474&postcount=23
gh0stslayer said:
Yeah man, I should get some sleep too. Been at this since 6 PM and its 4 AM now.
BTW here is the link for CMDProcessor I used
http://forum.xda-developers.com/showpost.php?p=40579474&postcount=23
Click to expand...
Click to collapse
My idea would be to add another run method to the SH class:
Code:
public CommandResult runWaitForWithProgress(final String s) {
s += "; echo This is the end of the command";
if (DEBUG)
Log.d(TAG, "runWaitFor( " + s + " )");
final Process process = run(s);
Integer exit_value = null;
String stdout = null;
String stderr = null;
if (process != null) {
try {
final DataInputStream dis = new DataInputStream(process.getInputStream());
while (true) {
if (dis.available() > 0) {
if (dis.readLine().equals("This is the end of the command")) {
break;
} else {
//TODO: Increase progress here
}
}
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
}
exit_value = process.waitFor();
stdout = getStreamLines(process.getInputStream());
stderr = getStreamLines(process.getErrorStream());
} catch (final InterruptedException e) {
Log.e(TAG, "runWaitFor " + e.toString());
} catch (final NullPointerException e) {
Log.e(TAG, "runWaitFor " + e.toString());
}
}
return new CommandResult(exit_value, stdout, stderr);
}
I've written this in a text editor and haven't tested it. So no guarantee that it will work. You'll probably have to fix some errors. But it should explain my idea.
nikwen said:
My idea would be to add another run method to the SH class:
Code:
public CommandResult runWaitForWithProgress(final String s) {
s += "; echo This is the end of the command";
if (DEBUG)
Log.d(TAG, "runWaitFor( " + s + " )");
final Process process = run(s);
Integer exit_value = null;
String stdout = null;
String stderr = null;
if (process != null) {
try {
final DataInputStream dis = new DataInputStream(process.getInputStream());
while (true) {
if (dis.available() > 0) {
if (dis.readLine().equals("This is the end of the command")) {
break;
} else {
//TODO: Increase progress here
}
}
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
}
exit_value = process.waitFor();
stdout = getStreamLines(process.getInputStream());
stderr = getStreamLines(process.getErrorStream());
} catch (final InterruptedException e) {
Log.e(TAG, "runWaitFor " + e.toString());
} catch (final NullPointerException e) {
Log.e(TAG, "runWaitFor " + e.toString());
}
}
return new CommandResult(exit_value, stdout, stderr);
}
I've written this in a text editor and haven't tested it. So no guarantee that it will work. You'll probably have to fix some errors. But it should explain my idea.
Click to expand...
Click to collapse
Is there a way to assign the output given by
Code:
ls /data/app/ | wc -l
to a variable ???
gh0stslayer said:
Is there a way to assign the output given by
Code:
ls /data/app/ | wc -l
to a variable ???
Click to expand...
Click to collapse
In a script or in Java?
nikwen said:
In a script or in Java?
Click to expand...
Click to collapse
In Java :silly:
I m trying to write the output of the command to a text file in sdcard
Code:
cmd.su.runWaitFor("ls /data/app/ | wc -l > /sdcard/bck.txt");
My aim is to use it to store the no of apps from the text file to a variable , and then use it for the progressbar .
Although It writes a text file with the no of apps as its content, i am unable to assign it to a variable in java
Code:
FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File("sdcard/backovery/bck.txt"));
bis = new BufferedInputStream(is);
nFiles = bis.read();
}catch (IOException e) {
e.printStackTrace();
tv.setText(""+nFiles);
It works but shows the value for nFiles in textview as 0 .
But it will be less messy if I could just store the shell command's output to a variable instead of storing it on a text file .