Android: Requesting root access in your app
16lz
2022-03-16
转帖:http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/
This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root access will only work if your phone allows it, or it has been ‘rooted’ (hacked to allow superuser permissions).
view plain copy to clipboard print ?- Processp;
- try{
- //Preformsutogetrootprivledges
- p=Runtime.getRuntime().exec("su");
- //Attempttowriteafiletoaroot-only
- DataOutputStreamos=newDataOutputStream(p.getOutputStream());
- os.writeBytes("echo/"DoIhaveroot?/">/system/sd/temporary.txt/n");
- //Closetheterminal
- os.writeBytes("exit/n");
- os.flush();
- try{
- p.waitFor();
- if(p.exitValue()!=255){
- //TODOCodetorunonsuccess
- toastMessage("root");
- }
- else{
- //TODOCodetorunonunsuccessful
- toastMessage("notroot");
- }
- }catch(InterruptedExceptione){
- //TODOCodetorunininterruptedexception
- toastMessage("notroot");
- }
- }catch(IOExceptione){
- //TODOCodetorunininput/outputexception
- toastMessage("notroot");
- }
Where my “toastMessage” is just a function which creates a toast to display on the screen. On phones with superuser permissions installed (root access) this will display a dialog asking the user to allow or deny the application permission to have root access:
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer