最近搬了家,还没来得及装上宽带,于是乎手机上用**万能钥匙偷偷连接了几个WIFI,可是电脑也想用的话,就必须借助工具来查看密码了,之前一贯的做法是使用RE管理器进入系统文件夹查看WIFI配置文件(/data/misc/wifi/wpa_supplicant.conf),后来嫌麻烦又下载了好些WIFI密码查看器,但始终不满意,广告太多了哈哈!
刚好最近自学了不少安卓相关的知识,何不自己实现一个呢?
先来看看成果图吧:
要实现这样的功能也非常简单,就是读取手机中的/data/misc/wifi/wpa_supplicant.conf文件,解析成需要的格式数据最好再显示在界面上就可以了
需要注意的是,读取该文件需要手机拥有root权限,同时该应用也要获取授权,通过android程序获取root权限,然后su用户执行命令行的方式读取即可。
这里我使用了开源的工具类:ShellUtils.java [https://github.com/wuwz/android-utils/blob/master/app/src/main/java/com/ihongqiqu/util/ShellUtils.java]
1、首先设计简单的javabean,用于存放解析的WIFI数据:
2、在Actity的onStart()事件中读取WIFI数据并通过适配器填充到ListView中
package org.wuwz.wifiviewer;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.wuwz.wifiviewer.adapter.WifiInfoAdapter;
import org.wuwz.wifiviewer.model.WifiInfo;
import org.wuwz.wifiviewer.utils.ShellUtils;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends Activity {
private ListView _lvWifiInfo = null;
private LinearLayout _layoutFullMsg = null;
private TextView _tvFullMsg = null;
private boolean _isReadCompile = false;
private List<WifiInfo> _dataList = new ArrayList<>();
private WifiInfoAdapter _wifiInfoAdapter;
private ClipboardManager _clipboardManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
_clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
}
private void initViews() {
_lvWifiInfo = (ListView) findViewById(R.id.listview_wifi_info);
_wifiInfoAdapter = new WifiInfoAdapter(this, _dataList);
_lvWifiInfo.setAdapter(_wifiInfoAdapter);
/*_lvWifiInfo.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "show details dialog..", Toast.LENGTH_SHORT).show();
return false;
}
});*/
_lvWifiInfo.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
// show details menu dialog..
menu.setHeaderTitle("选择操作");
menu.add(v.getId(), 0, 0, "复制WIFI密码");
menu.add(v.getId(), 1, 1, "复制WIFI名字+密码");
}
});
_tvFullMsg = (TextView) findViewById(R.id.tv_full_msg);
_layoutFullMsg = (LinearLayout) findViewById(R.id.layout_full_msg);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
WifiInfo data = _dataList.get(info.position);
if (data.getWifiPwd().equals(getString(R.string.wifi_no_pwd))) {
Toast.makeText(MainActivity.this, "这个WIFI没有密码,换一个试试吧!", Toast.LENGTH_LONG).show();
} else {
String content = "";
switch (item.getItemId()) {
case 0:
content = data.getWifiPwd();
break;
case 1:
content = MessageFormat.format("WIFI名字:{0} 密码:{1}", data.getWifiName(), data.getWifiPwd());
break;
}
_clipboardManager.setPrimaryClip(ClipData.newPlainText(null, content));
Toast.makeText(MainActivity.this, "操作成功!", Toast.LENGTH_LONG).show();
}
return super.onContextItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
if (!_isReadCompile) {
readWifiConfigToListView();
}
}
private void readWifiConfigToListView() {
String[] commands = new String[]{"cat /data/misc/wifi/wpa_supplicant.conf\n", "exit\n"};
ShellUtils.CommandResult cr = ShellUtils.execCommand(commands, true, true);
if (cr.result != 0) {
_tvFullMsg.setText(getString(R.string.analysis_error_msg));
_layoutFullMsg.setVisibility(View.VISIBLE);
Toast.makeText(this, getString(R.string.analysis_error_msg), Toast.LENGTH_LONG).show();
return;
}
String wifiConfigInfo = String.valueOf(cr.successMsg);
if (TextUtils.isEmpty(wifiConfigInfo)) {
// no root.
_layoutFullMsg.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, getString(R.string.no_root_msg), Toast.LENGTH_LONG).show();
return;
}
// analysis result.
Pattern network = Pattern.compile("network=\\{([^\\}]+)\\}", Pattern.DOTALL);
Matcher matcher = network.matcher(wifiConfigInfo);
WifiInfo model;
while (matcher.find()) {
String networkBlock = matcher.group();
Pattern ssid = Pattern.compile("ssid=\"([^\"]+)\"");
Matcher ssidMatcher = ssid.matcher(networkBlock);
if (ssidMatcher.find()) {
model = new WifiInfo();
model.setWifiName(ssidMatcher.group(1));
Pattern psk = Pattern.compile("psk=\"([^\"]+)\"");
Matcher pskMatcher = psk.matcher(networkBlock);
model.setWifiPwd(pskMatcher.find() ? pskMatcher.group(1) : getString(R.string.wifi_no_pwd));
_dataList.add(model);
}
}
Collections.reverse(_dataList);
_wifiInfoAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, getString(R.string.welcome_msg), Toast.LENGTH_SHORT).show();
_isReadCompile = true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_about) {
Uri uri = Uri.parse("http://github.com/wuwz");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
return true;
} else if(id == R.id.action_exit) {
finish();
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
private long _exitTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - _exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
_exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
至此,整个程序就已经基本完工了,从此就再也不用别人做的广告一大堆的WIFI密码查看器啦!
项目源码地址:https://github.com/wuwz/WiFiViewer
原文链接:https://www.qiquanji.com/post/8882.html
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
微信扫码关注
更新实时通知