转载http://www.open-open.com/lib/view/open1332033368218.html
前几天在做一个文件管理器,在打开非sdcard下的目录文件时碰到了空指针的错误,色友说要获取root权限才能访问其他需root权限的文件夹,于是用了下面的方法获取权限,
public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授权root权限命令
/**
* 授权root用户权限
*
* @param command
* */
public boolean rootCommand(String command) {
Process process = null;
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(command+"\n");
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (dos != null) {
dos.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
虽然调用成功,但是还是空指针错误,如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
// loadApps();
rootCommand(rootPowerCommand);//调用获取root权限
initTool();
initFileList();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) this.getListAdapter()
.getItem(position);
FileBean fileBean = (FileBean) map.get("icon");
Log.v("--------path---------", fileBean.getPath());
File file = new File(fileBean.getPath());//此处路劲fileBean.getPath()经调试得到是存在的目录,如我点击root文件夹得到/root
if (!file.isDirectory()) {
fileControl.openFile(file);// 打开文件
} else {
fileDirControl.openDir(file);// 打开文件夹。。。。。。。。。。。。。。。。接下面
}
}
/**
* 打开目录
*
* @param file
* */
public void openDir(File file) {
fileBroswer.current_path = file.getAbsolutePath();
fileBroswer.currentDir.setText(file.getAbsolutePath());
File[] files = file.listFiles();//得到的files竟然是空的,就是说虽然目录文件存在,但是你不能访问它,
data = fileBroswer.getData(files);//由此也就照成了空指针错误,为什么么?求解释啊。。。。。。
MyAdapter myAdapter = new MyAdapter(context, data);
fileBroswer.setListAdapter(myAdapter);
}
http://www.jb51.net/article/43411.htm Android获取ROOT权限的实例代码
这篇文章主要介绍了Android如何获取ROOT权限,写了一个小方法,大家可以在应用中检测ROOT权限获取Android的ROOT权限其实很简单,只要在Runtime下执行命令"su"就可以了。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
// 获取ROOT权限
public
void
get_root(){
if
(is_root()){
Toast.makeText(mCtx,
"已经具有ROOT权限!"
, Toast.LENGTH_LONG).show();
}
else
{
try
{
progress_dialog = ProgressDialog.show(mCtx,
"ROOT"
,
"正在获取ROOT权限..."
,
true
,
false
);
Runtime.getRuntime().exec(
"su"
);
}
catch
(Exception e){
Toast.makeText(mCtx,
"获取ROOT权限时出错!"
, Toast.LENGTH_LONG).show();
}
}
}
/*其中is_root()判断是否已经具有了ROOT权限。只要/system/bin/su、/system/xbin/su这两个文件中有一个存在,就表明已经具有ROOT权限,如果两个都不存在,则不具有ROOT权限。*/
// 判断是否具有ROOT权限
public
static
boolean
is_root(){
boolean
res =
false
;
try
{
if
((!
new
File(
"/system/bin/su"
).exists()) &&
(!
new
File(
"/system/xbin/su"
).exists())){
res =
false
;
}
else
{
res =
true
;
};
}
catch
(Exception e) {
}
return
res;
|
Android用代码获取手机root之后的最高权限http://www.jb51.net/article/34410.htm
用代码获取最高权限首先手机得root,没有root请不要往下看。
首先是写一个静态方法 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
static
boolean
isRoot(String pkgCodePath) {
Process process =
null
;
DataOutputStream os =
null
;
try
{
String cmd =
"chmod 777 "
+ pkgCodePath;
process = Runtime.getRuntime().exec(
"su"
);
// 切换到root帐号
os =
new
DataOutputStream(process.getOutputStream());
os.writeBytes(cmd +
"\n"
);
os.writeBytes(
"exit\n"
);
os.flush();
process.waitFor();
}
catch
(Exception e) {
return
false
;
}
finally
{
try
{
if
(os !=
null
) {
os.close();
}
process.destroy();
}
catch
(Exception e) {
}
}
return
true
;
}
|
这个方法返回true和false。
调用的时候代码如下:
isRoot(getPackageCodePath());
这样手机上就会提示此程序要获取最高权限 是否同意,如果点是那么就获取到了。
如果想知道结果可以这么写 :
boolean rootResult=isRoot(getPackageCodePath());