欢迎大家来到IT世界,在知识的湖畔探索吧!
功能分析
1、通过Runtime.getRuntime().exec(“su”)获取root权限。
2、通过process.getOutputStream()和process.getInputStream()获取终端的输入流和输出流。
3、通过dataOutputStream.writeBytes(“cat /data/misc/wifi/*.conf\n”)往终端中输入命令。注意,这里必须要有\n作为换行,否则会与后一个exit命令作为一个命令,最终导致命令执行失败,无法得到结果。
4、通过dataInputStream获取命令执行结果,并以UTF-8的编码转换成字符串。
5、使用正则表达式过滤出wifi的用户名和密码。
操作步骤
1、新建一个项目。这就不用多说了吧!然后打开MainActivity.java
2、新建一个WiFiManager类
3、然后新建一个WiFiInfo类
新建2个属性 ssid和password分别代表WiFi名称和WiFi密码
4、在WiFiInfo类中加入以下代码
package com.example.honglinchen.wifiviewpassword; public class WiFiInfo { /* * WiFi 名称 */ public String ssid=""; /* * WiFi 密码 */ public String password=""; }
欢迎大家来到IT世界,在知识的湖畔探索吧!
5、然后找到res目录下activity_main.xml布局文件
欢迎大家来到IT世界,在知识的湖畔探索吧!<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="com.example.honglinchen.wifiviewpassword.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dip" android:background="#66CD00" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textStyle="normal" android:text="获取已经连接WiFi的密码!" android:textColor="#ffffff" /> </RelativeLayout> <ListView android:id="@+id/WifiInfoView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
6、然后在WiFiManage添加如下代码:
package com.example.honglinchen.wifiviewpassword; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WifiManage { public List<WiFiInfo> Read() throws Exception { List<WiFiInfo> wifiInfos=new ArrayList<WiFiInfo>(); Process process = null; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; StringBuffer wifiConf = new StringBuffer(); try { process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); dataInputStream = new DataInputStream(process.getInputStream()); dataOutputStream .writeBytes("cat /data/misc/wifi/*.conf\n"); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); InputStreamReader inputStreamReader = new InputStreamReader( dataInputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); String line = null; while ((line = bufferedReader.readLine()) != null) { wifiConf.append(line); } bufferedReader.close(); inputStreamReader.close(); process.waitFor(); } catch (Exception e) { throw e; } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (dataInputStream != null) { dataInputStream.close(); } process.destroy(); } catch (Exception e) { throw e; } } Pattern network = Pattern.compile("network=\\{([^\\}]+)\\}", Pattern.DOTALL); Matcher networkMatcher = network.matcher(wifiConf.toString() ); while (networkMatcher.find() ) { String networkBlock = networkMatcher.group(); Pattern ssid = Pattern.compile("ssid=\"([^\"]+)\""); Matcher ssidMatcher = ssid.matcher(networkBlock); if (ssidMatcher.find() ) { WiFiInfo wifiInfo=new WiFiInfo(); wifiInfo.ssid=ssidMatcher.group(1); Pattern psk = Pattern.compile("psk=\"([^\"]+)\""); Matcher pskMatcher = psk.matcher(networkBlock); if (pskMatcher.find() ) { wifiInfo.password=pskMatcher.group(1); } else { wifiInfo.password="无密码"; } wifiInfos.add(wifiInfo); } } return wifiInfos; } }
7、在MainActivity.java中开始编写如下代码:
欢迎大家来到IT世界,在知识的湖畔探索吧!package com.example.honglinchen.wifiviewpassword; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { private WifiManage wifiManage; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); wifiManage = new WifiManage(); try { Init(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void Init() throws Exception { List<WiFiInfo> wifiInfos = wifiManage.Read(); ListView wifiInfosView=(ListView)findViewById(R.id.WifiInfoView); WifiAdapter ad = new WifiAdapter(wifiInfos,MainActivity.this); wifiInfosView.setAdapter(ad); } public class WifiAdapter extends BaseAdapter { List<WiFiInfo> wifiInfo =null; Context con; public WifiAdapter(List<WiFiInfo> wifiInfos,Context con){ this.wifiInfo =wifiInfos; this.con = con; } @Override public int getCount() { // TODO Auto-generated method stub return wifiInfo.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return wifiInfo.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = LayoutInflater.from(con).inflate(android.R.layout.simple_list_item_1, null); TextView tv = (TextView)convertView.findViewById(android.R.id.text1); tv.setText("Wifi:"+wifiInfo.get(position).ssid+"\n密码:"+wifiInfo.get(position).password); return convertView; } } }
完功。跑起来看看吧!
如下图:刚写好,测试好,发给大家吧!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/34859.html