Hi,
I'm writing an android application which have to run a android shell script as root. But I don't know how to do that . Can anyone help me?
It's a very simple application with a button in the middle of the screen and if I press that button the script should be executed as root. :good:
Use
Process p = Runtime.getRuntime().exec("su");
p.getOutputStream().write("your command".getBytes());
EmptinessFiller said:
Use
Process p = Runtime.getRuntime().exec("su");
p.getOutputStream().write("your command".getBytes());
Click to expand...
Click to collapse
Thank you for tour reply, :good:
I have one more question. How can i show the output of the script to the user in something like a popup message?
supergerrit said:
Thank you for tour reply, :good:
I have one more question. How can i show the output of the script to the user in something like a popup message?
Click to expand...
Click to collapse
You can use superuser libraries like libSuperuser by chainfire, or RootTools by stericsson to execute the script which can handle the output for you. You can assign the output to a String and then display it in a dialogbox.
See these links -
How to SU
RootTools
supergerrit said:
Thank you for tour reply, :good:
I have one more question. How can i show the output of the script to the user in something like a popup message?
Click to expand...
Click to collapse
You would need
say if your prcess is Process p
Scanner s = new Scanner(p.getInputStream)
while(s.hasNextLine){
//s.nextLine() will give the process output
}
Sent from my GT-S5302 using Tapatalk 2
gh0stslayer said:
You can use superuser libraries like libSuperuser by chainfire, or RootTools by stericsson to execute the script which can handle the output for you. You can assign the output to a String and then display it in a dialogbox.
See these links -
How to SU
RootTools
Click to expand...
Click to collapse
Thank you so much for your reply. The links helped me so much! :good:
But i have one more question:
Can i also run multi line shell schripts from a file in the assets folder?
Example: i have a file: 'scripfile'. I put this one in the assets folder of the application. How can i run that file?
supergerrit said:
Thank you so much for your reply. The links helped me so much! :good:
But i have one more question:
Can i also run multi line shell schripts from a file in the assets folder?
Example: i have a file: 'scripfile'. I put this one in the assets folder of the application. How can i run that file?
Click to expand...
Click to collapse
I can think of one way to do it. Like sak-venom1997 posted above, you can read the file one line at a time and save it to a String array or a list and pass the String array to the superuser library
PHP:
List<String> Shell.SH.run(List<String> commands)
or
List<String> Shell.SH.run(String[] commands)
Will it work for non rooted devices??
sak-venom1997 said:
You would need
say if your prcess is Process p
Scanner s = new Scanner(p.getInputStream)
while(s.hasNextLine){
//s.nextLine() will give the process output
}
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
Will the codes be applicable for non rooted device? or it will run on all the devices..?
If it is for non rooted... then can you please suggest me how can I access shell.. or run shell commands without rooting the device.
nitinsao said:
Will the codes be applicable for non rooted device? or it will run on all the devices..?
If it is for non rooted... then can you please suggest me how can I access shell.. or run shell commands without rooting the device.
Click to expand...
Click to collapse
If you take a look here
http://su.chainfire.eu/#how-call
you will notice this-
PHP:
List<String> Shell.SH.run(String command)
which I believe can be used for executing shell without root. Now I haven't tried it, so I am not sure, but it's worth a shot.
Related
I am new to programming Java. Can anyone help me with source code for a simple android app that copies one file from sdcard to any directory in Phone. I have eclipse and emulator setup for testing.
cheers
Im not into java... but you could write a small bash script!
Bashs can be run from a terminal (connectbot, irrsi, ssh-server)
Or you simply use the app (GScript), where you can add your own scripts and run them just with 1 click!
Code:
#!/system/bin/sh
file=/sdcard/FOLDER_OF_FILE/FILES_I_WANT_TO_COPY
target=/TARGET_FOR_FILE
cp -frv $file $target
Hope I could help you...
I would try searching stackoverflow.com ....
.... be prepared though, file operations in Android are not a quick 2 lines of code.
sebsch1991 said:
Im not into java... but you could write a small bash script!
Bashs can be run from a terminal (connectbot, irrsi, ssh-server)
Or you simply use the app (GScript), where you can add your own scripts and run them just with 1 click!
Code:
#!/system/bin/sh
file=/sdcard/FOLDER_OF_FILE/FILES_I_WANT_TO_COPY
target=/TARGET_FOR_FILE
cp -frv $file $target
Hope I could help you...
Click to expand...
Click to collapse
Thanks for reply. But i am looking for a pure android app and not script or adb commands via terminal !
@eatmold
eatmold said:
I would try searching stackoverflow.com ....
.... be prepared though, file operations in Android are not a quick 2 lines of code.
Click to expand...
Click to collapse
Thanks a lot man, i look forward to see some code soon .....
user_000 said:
I am new to programming Java. Can anyone help me with source code for a simple android app that copies one file from sdcard to any directory in Phone. I have eclipse and emulator setup for testing.
cheers
Click to expand...
Click to collapse
First of all, you can't copy a file to any directory on the phone. The app can only write to directories to which it has permission to write. Second, some parts of the phone can't be written to at all because the filesystem is read-only. Third, file I/O is non-trivial in the dalvik VM. It would take a substantial amount of code to do what you want.
Do you have some goal in mind for this starter project? There is the "Hello World" project at http://developer.android.com if you just want some example code.
Gene Poole said:
First of all, you can't copy a file to any directory on the phone. The app can only write to directories to which it has permission to write. Second, some parts of the phone can't be written to at all because the filesystem is read-only. Third, file I/O is non-trivial in the dalvik VM. It would take a substantial amount of code to do what you want.
Do you have some goal in mind for this starter project? There is the "Hello World" project at http://developer.android.com if you just want some example code.
Click to expand...
Click to collapse
Well the aim is to get an idea of how a very simple app can work on phone. So it can even be copying a file from one directory of SDcard to another directory on SDcard on Phone. And also lets assume that we have the permission to write to any directory on phone (i.e superuser or its writable etc).
Yea 'hello world' is a nice example and on the same lines a simple non-gui app to copy file is what i am looking for. It will give a good overview of how to do some basic I/O operation on phone via android app.
cheers
If you want to do this as a Java learning experience, then you'll probably want to handle the file I/O using strictly Java which can be a real pain. Nonetheless, all you need is here:
http://developer.android.com/reference/java/io/package-summary.html
although, typically things like file I/O are best left to the operating system which is usually more optimized for that sort of thing--especially when the OS is *nix. To leverage the OS to do your dirty work natively, you can use the Runtime class:
http://developer.android.com/reference/java/lang/Runtime.html
Thanks for the references. But interest is to see a working code on an Android device and not Java as a whole. So a simple app source code can be a really good starting point. Hello World is good first example but does not include I/O, so someone with experience can lead the newbies in a good way by showing a simple example.
Come on guys share some experience !
ROM Porter
About the tool:
This tool is based on rishab.raj37's great guide. And makes porting ROMs in few simple clicks. This is made only for linux.
Click to expand...
Click to collapse
How to use it:
1. Download the port.sh from here.
2. Place the ROM zips and script in the home directory (~/) (home/username).
3. Now open the terminal and type the following:
Code:
./port.sh
In some cases this might not work so try this:
Code:
bash ~/port.sh
4. Now follow the on-screen instructions and done. :good:
5. You can find your ported ROM in ~/PORT/ directory.
6. Enjoy and press THANKS.
Click to expand...
Click to collapse
Note:
If you get stuck on bootloop, try this tool, here.
Click to expand...
Click to collapse
Credits:
rishab.raj37- For such a great guide
ME
Click to expand...
Click to collapse
Reserved for FAQs
THANKS for the Awesome Tool
Witch program to use for build.prop file to be open?
djkidy123 said:
Witch program to use for build.prop file to be open?
Click to expand...
Click to collapse
Notepad++?
Regards
N
In ubuntu??
OK they are open in GEDIT, but i'm getting only blank pages with red line can't open...
Seeing the code, yes it does open in gedit. Sorry.
Regards
N
djkidy123 said:
In ubuntu??
OK they are open in GEDIT, but i'm getting only blank pages with red line can't open...
Click to expand...
Click to collapse
Use SciTE .
www.scintilla.org/SciTE.html
karandpr said:
Use SciTE .
www.scintilla.org/SciTE.html
Click to expand...
Click to collapse
Thanx, but how to setup script to auto open in this reader?!
djkidy123 said:
Thanx, but how to setup script to auto open in this reader?!
Click to expand...
Click to collapse
Open port.sh find and replace gedit with path-to-scite( if its default path, just scite)
Regards
N
So this does not work with gedit at all?
The script keeps trying to extract the rom files into the same exact folder, causing conflicts. Also, when it tries to open the build.prop files it thinks they are in folders named after the ROM zips that are never created. Has anyone gotten this to work yet?
Download link inactive
Abhinav1997 said:
ROM Porter
About the tool:
How to use it:
Note:
Credits:
Click to expand...
Click to collapse
Thank you very much for this helpful tool. But th download link does'nt work for me. Could you help me please by giving another link or sending the file in mp.
Thanks again for your help
Is impossible to download from dev host!!!!
Hi all!
I want to read and write /data but when i try to acces, it says "permision denied" because i need root rights.
How can i do it ? Thanks and sorry for my bad english
Sent from my GT-S5830 using xda premium
JorX_AD said:
Hi all!
I want to read and write /data but when i try to acces, it says "permision denied" because i need root rights.
How can i do it ? Thanks and sorry for my bad english
Sent from my GT-S5830 using xda premium
Click to expand...
Click to collapse
Here you go: http://forum.xda-developers.com/showthread.php?t=2226664
Thanks to nikwen, There is a very easy way in which we use the RootTools library.
Here it is.
If you want to know if you have access to root or know if device is rooted, here's the code:
Code:
if (RootTools.isRootAvailable() && RootTools.IsAccessGiven()){
// So the device is rooted and you have access to Root.
} else {
// Either the device is not rooted or your app doesn't have access to Root.
}
Also don't forget to include SuperUser permission in your manifest.xml
Code:
<permission name=".ACCESS_SUPERUSER"/>
torpedo mohammadi said:
Thanks to nikwen, There is a very easy way in which we use the RootTools library.
Here it is.
If you want to know if you have access to root or know if device is rooted, here's the code:
Code:
if (RootTools.isRootAvailable() && RootTools.IsAccessGiven()){
// So the device is rooted and you have access to Root.
} else {
// Either the device is not rooted or your app doesn't have access to Root.
}
Also don't forget to include SuperUser permission in your manifest.xml
Code:
<permission name=".ACCESS_SUPERUSER"/>
Click to expand...
Click to collapse
The best RootTools tutorial can in my opinion be found on the wiki page of the project: https://code.google.com/p/roottools/wiki/Usage
I really recommend RootTools as it is MUCH easier than doing everything on your own, especially if your app needs SU access more than once.
I just need help at best way for me to build a simple app to request for root, then dd install a kernel
dd if=boot.img of=/dev/block/mmcblk0p14
and then give option to reboot.
Thanks for any advice
carl1961 said:
I just need help at best way for me to build a simple app to request for root, then dd install a kernel
dd if=boot.img of=/dev/block/mmcblk0p14
and then give option to reboot.
Thanks for any advice
Click to expand...
Click to collapse
Just a tip, I'm assuming this app will work only for your device or similar partitioned devices. You could read the mounts file under proc using java and extract the Mount path instead of hard coding it. And executing shell commands can be done using JNI system() calls or from java it's Runtime.getRuntime().exec(ddcommandhere);
MasterAwesome said:
Just a tip, I'm assuming this app will work only for your device or similar partitioned devices. You could read the mounts file under proc using java and extract the Mount path instead of hard coding it. And executing shell commands can be done using JNI system() calls or from java it's Runtime.getRuntime().exec(ddcommandhere);
Click to expand...
Click to collapse
thanks for the advice, my issue right know is still l amd still learning android studio. I cannot figure out how and where to use this command. all apk guides I find is for like online and hello world LOL
carl1961 said:
thanks for the advice, my issue right know is still l amd still learning android studio. I cannot figure out how and where to use this command. all apk guides I find is for like online and hello world LOL
Click to expand...
Click to collapse
Well, let android studio create a project for you, you'll have an activity there run your code from there on another thread that's not the main ui thread.
carl1961 said:
thanks for the advice, my issue right know is still l amd still learning android studio. I cannot figure out how and where to use this command. all apk guides I find is for like online and hello world LOL
Click to expand...
Click to collapse
perhaps you can just use a button in your MainActivity
And override the onClickListener
Then put that command(RunTime.getRunTime().exec().. in that method. Easy
Sent from my A0001 using Tapatalk 2
Vivek_Neel said:
perhaps you can just use a button in your MainActivity
And override the onClickListener
Then put that command(RunTime.getRunTime().exec().. in that method. Easy
Sent from my A0001 using Tapatalk 2
Click to expand...
Click to collapse
Thanks @Vivek_Neel, might get time this weekend to try that
Hello,
I have developed a quite simple perl script (parse some files, make lists, remove other files) and would like to be able to run it from my phone rather than copying the working directory to my pc and then execute the script.
Could someone please give me some light on how to do? If I write this in shell script, would it be more convenient?
Thanks in advance
Br1ce said:
Hello,
I have developed a quite simple perl script (parse some files, make lists, remove other files) and would like to be able to run it from my phone rather than copying the working directory to my pc and then execute the script.
Could someone please give me some light on how to do? If I write this in shell script, would it be more convenient?
Thanks in advance
Click to expand...
Click to collapse
Yes, if you make it into a shell script you can do anything with it. You can simply drop into your /system/bin and then call it from any folder.
Heisenberg420 said:
Yes, if you make it into a shell script you can do anything with it. You can simply drop into your /system/bin and then call it from any folder.
Click to expand...
Click to collapse
I guess I have to try then. Thanks!
Br1ce said:
I guess I have to try then. Thanks!
Click to expand...
Click to collapse
Might be worth it to take a look at this:
https://play.google.com/store/apps/details?id=org.androidideas.scriptlauncher
Actually after discussing this with a friend, I' m going to develop a complete application that will have other features. These scripts are doing quite basic stuff so it should be doable.
SL4A looks interesting though, I might still play a little bit with it