安卓系统下的目录权限问题

安卓系统在连接wifi的时候会保存连接的密码,这一配置信息保存在/data/misc/wifi/wpa_supplicant.conf目录下。

但是这个目录需要Root权限才能读取,但是有个问题是很多root了用户打开data目录的时候是空的,用rootexplore打开也是空的

我手中的一个百度小鸟平板用的4.2.2的系统就存在这个问题。

用adb shell命令打开平板目录尝试

shell@viewsonic82_6122:/ $ ls /data
ls /data
opendir failed, Permission denied
可以看到/data目录下的ls 命令被禁用了

看下面这个命令

shell@viewsonic82_6122:/ $ cd /data
cd /data
shell@viewsonic82_6122:/data $ cd misc
cd misc
shell@viewsonic82_6122:/data/misc $ cd wifi
cd wifi
/system/bin/sh: cd: /data/misc/wifi: Permission denied
可以看到cd命令的前两层命令正常,再往后面就被禁止使用cd命令了。

这个目录其实是存在的,我们可以通过root把目录权限改掉,让它暂时能访问。

下面是代码,其中获得目录root权限的代码是直接拿过来用的,在此表示感谢。

1.Activity的代码

package com.example.readwificfg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.readwificfg.service.getRoot;

public class MainActivity extends Activity {
	
	private TextView tv_result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv_result = (TextView) findViewById(R.id.tv_result);	
		
		//获得前两个路径的ROOT权限
		String path="/data/misc/wifi";			
		getRoot.upgradeRootPermission(path);
		path="/data/misc/wifi/wpa_supplicant.conf";		
		getRoot.upgradeRootPermission(path);
	}	
	
	public void readwificfg(View view){
		try {
			//逐行读取wpa_supplicant.conf里的文本内容,存到字符串result中
			File file = new File(
					"/data/misc/wifi/wpa_supplicant.conf");
			FileInputStream fls=new FileInputStream(file);
			BufferedReader br=new BufferedReader(new InputStreamReader(fls));
			String line=null;
			StringBuilder result=new StringBuilder();
			while((line=br.readLine())!=null){
				result.append(line);
				result.append("\n");
			}
			br.close();
			//把读出来的字符串显示到长文本框中
			tv_result.setText(result.toString());
			Toast.makeText(this, "读取wifi配置文件成功",Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this, "读取wifi配置文件失败,请确保取得ROOT权限", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void readwificfg2(View view){
		try {
			//创建WifiManager实例 
			WifiManager wfm=(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
			//获得配置信息的List
			List<WifiConfiguration> configs=wfm.getConfiguredNetworks();
			StringBuilder result=new StringBuilder();
			String head="SSID"+"\t"+"密码"+"\n";
			result.append(head);
			//读每一个配置信息,并加到字符串
			for(WifiConfiguration config:configs){
				String str = config.SSID+"\t"+config.preSharedKey+"\n";
				result.append(str);				
			}
			tv_result.setText(result.toString());
			String size=String.valueOf(configs.size());
			Toast.makeText(this, "读取wifi配置,节点数目"+size, Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this, "读取wifi配置失败", Toast.LENGTH_SHORT).show();
		}
	}

	

}

2.别人的获得root权限的代码

package com.example.readwificfg.service;

import java.io.DataOutputStream;

public class getRoot {

	
	/** 
	 * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限) 
	 *  
	 * @return 应用程序是/否获取Root权限 
	 */  
	public static boolean upgradeRootPermission(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;  
	}
}
3.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="readwificfg"
        android:text="从固定路径读取wifi配置文件" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="readwificfg2"
        android:text="从API读取wifi配置,密码为*不可读" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TextView
            android:id="@+id/tv_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

再说明一下,上面的代码中第一个按钮实现的是从目录读取的,能读到配置文件,前提是要有root权限,root权限要执行两次,这里懒得改了

第二个按钮的代码使用API,但是拿到的密码是一个星号,API存的时候是把字符串存进去了,但是返回的时候返回*,第二个按钮的代码在我的2.2.2的系统上运行正常,4.2.2的系统读取失败



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值