Recently moved home, have not had time to install broadband, and so on the phone with * * Master key secretly connected to a few wifi, but the computer also want to use, you must use the tool to view the password, it is always the practice of using the RE manager into the System folder to view the WiFi profile (/data/misc/ wifi/wpa_supplicant.conf), later bothered to download a lot of WiFi password viewer, but always not satisfied, advertising too much haha!
Just recently self-taught a lot of Android-related knowledge, why not implement one yourself?
Let's take a look at the results chart:
The interface layout is very simple, the whole program has only one activity, using Linearlayout+listview, too simple to paste the code
To achieve such a function is also very simple, is to read the /data/misc/wifi/wpa_supplicant.conf file in the phone, parsing into the required format data is best to display the interface on the
It is important to note that reading this file requires the phone to have root permissions, and the application also to obtain authorization, referring to the practice of the Internet gods, the Android program to get root permissions, and then SU users to execute command line to read the way.
1. First design a simple javabean for storing the parsed WiFi data:
Package Org.wuwz.wifi.model; Public class Wifimodel { private String name; Private String password; // getter and setter:}
2. Read the WiFi data in the actity OnStart () event and populate it with an adapter into the ListView
Private BooleanIsreadcomplete =false; @Overrideprotected voidOnStart () {Super. OnStart (); if(!isreadcomplete) {//avoid repeated readsReadwificonfig (); }}Private voidReadwificonfig () {process process=NULL; DataOutputStream out=NULL; DataInputStream in=NULL; StringBuffer result=NewStringBuffer (); Try { //Request Root PermissionProcess = Runtime.getruntime (). EXEC ("su"); out=NewDataOutputStream (Process.getoutputstream ()); Inch=NewDataInputStream (Process.getinputstream ()); //to read a WiFi profile using the command lineOut.writebytes ("cat/data/misc/wifi/wpa_supplicant.conf\n"); Out.writebytes ("Exit\n"); Out.flush (); InputStreamReader Inreader=NewInputStreamReader (In, "UTF-8"); BufferedReader Reader=NewBufferedReader (Inreader); String Line; while(line = Reader.readline ())! =NULL) {result.append (line); } reader.close (); Inreader.close (); Process.waitfor (); } Catch(Exception e) {log.d ("Wuwz", "Exception occurred:" +e.getmessage ()); } finally { Try { if(In! =NULL) {in.close (); } if(Out! =NULL) {out.close (); } } Catch(IOException e) {e.printstacktrace (); } String Wificonfiginfo=string.valueof (Result); if(Textutils.isempty (wificonfiginfo)) {NewAlertdialog.builder (mainactivity. This). Settitle (Prompted). Setmessage (R.string.no_root_tip). Setpositivebutton ("Exit program",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {System.exit (1); }}). Show (); return; } //Processing ResultsAnalysisresult (string.valueof (result)); }}//parsing ResultsPrivate voidAnalysisresult (String result) {Pattern network= Pattern.compile ("network=\\{([^\\}]+) \ \}", Pattern.dotall); Matcher Matcher=Network.matcher (Result); Wifimodel Model=NULL; while(Matcher.find ()) {String Networkblock=Matcher.group (); Pattern SSID= Pattern.compile ("ssid=\" ([^\ "]+) \" "); Matcher Ssidmatcher=Ssid.matcher (Networkblock); if(Ssidmatcher.find ()) {model=NewWifimodel (); Model.setname (Ssidmatcher.group (1)); Pattern PSK= Pattern.compile ("psk=\" ([^\ "]+) \" "); Matcher Pskmatcher=Psk.matcher (Networkblock); if(Pskmatcher.find ()) {Model.setpassword (Pskmatcher.group (1)); } Else{Model.setpassword (getString (R.string.no_pwd_tip)); } wifimodels.add (model); }} collections.reverse (Wifimodels); Wifiadapter.notifydatasetchanged (); Toast.maketext (mainactivity. This, "Welcome, long press WiFi password can be copied!" ", Toast.length_short). Show (); Isreadcomplete=true;}
At this point, the entire program has been basically completed, for the convenience of use, may wish to add a few features:
3. Add the ListView item long Press the copy password event
//Long Press copy passwordMlistview.setonitemlongclicklistener (NewAdapterview.onitemlongclicklistener () {@Override @SuppressLint ("Newapi") @SuppressWarnings ("Deprecation") Public BooleanOnitemlongclick (adapterview<?>Parent, view view,intIndexLongID) {String password=wifimodels.get (Index). GetPassword (); if(GetString (R.string.no_pwd_tip). Equals (password)) {Toast.maketext (mainactivity. This, "This wifi no password, change a try it!" ", Toast.length_short). Show (); return false; } //Clipboard ManagementClipboardmanager Climanager =(Clipboardmanager) Getsystemservice (Clipboard_service); Climanager.settext ("WIFI:" +wifimodels.get (Index). GetName () + "Password:" +password); Toast.maketext (mainactivity. This, "WiFi Password copy success, thanks for using!" ", Toast.length_short). Show (); return true; }});
4. Two consecutive return keys to exit the program
Private Longmexittime; @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if(KeyCode = =keyevent.keycode_back) { if((System.currenttimemillis ()-Mexittime) > 2000) {Toast.maketext ( This, "Press the return key again to exit the program", Toast.length_short). Show (); Mexittime=System.currenttimemillis (); } Else{system.exit (1); } return true; } return Super. OnKeyDown (KeyCode, event);}
From then on no other people do ads a lot of WiFi password viewer!
DIY WiFi Password Viewer