First, demand analysis
Recently the computer needs to connect WiFi, but found the WiFi password to forget. And the phone has saved the WiFi password, but in the phone's settings screen can not see.
Although there are already some apps that can see the WiFi password, the main worry is that the passwords will be uploaded to the background by those apps. or write yourself a more assured. and using the app to see just a click, it is more convenient than directly to find the file stored in the system password.
Second, the main function realization 2.1 read system files
The android system saved the WiFi password file saved in/data/misc/wifi/wpa_supplicant.conf [1], by running the command-line program ' Cat ' in code to read the file [3][4].
String Commandresult=commandforresult ("cat/data/misc/wifi/wpa_supplicant.conf"); Publicstring Commandforresult (String command) {Try{Process Process= Runtime.getruntime (). EXEC ("su"); DataOutputStream OutputStream=NULL; OutputStream=NewDataOutputStream (Process.getoutputstream ()); Outputstream.writebytes (Command+ "\ n"); Outputstream.flush (); Outputstream.writebytes ("Exit\n"); Outputstream.flush (); BufferedReader in=NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); StringBuilder Total=NewStringBuilder (); String Line; while(line = In.readline ())! =NULL) {total.append (line); Total.append ("\ n"); } returntotal.tostring (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); return"Error"; } }
2.2. Sort the WiFi information by priority from big to small
The WiFi information that is saved in the wpa_supplicant.conf file is mainly in the following format, with each network message starting with the net. Key_mgmt=none indicates that the network does not require a password. When KEY_MGMT=WPA-PSK, password information is identified through the PSK field.
Currently see the phone in the wpa_supplicant.conf file does not have a different network according to the priority value of sorting. And those who often use the network priority is relatively high, is placed behind the file, so consider the network information according to priorities from big to small to sort.
string sortbypriority (String input) {string [] stringperline=input.split ("\ n"); ArrayList<NetworkPara> list=NewArraylist<mainactivity.networkpara>(); intStart=0,end=0; Networkpara Networkpara=NULL; for(inti = 0; i < stringperline.length; i++) { if(Stringperline[i].contains ("network={") ) {Start=1; End=0; Networkpara=NewNetworkpara (); Networkpara.parastring=""; } if(start==1) { if(networkpara!=NULL) {networkpara.parastring=networkpara.parastring.concat (Stringperline[i]) + "\ n"; } if(Stringperline[i].contains ("Priority") {String []priosplit=stringperline[i].split ("="); Networkpara.priority=integer.parseint (priosplit[priosplit.length-1]); } if(Stringperline[i].contains ("}") ) {Start=0; End=1; } } if(end==1) {list.add (Networkpara); }} collections.sort (list,NewComparator () { Public intCompare (Object O1, Object O2) {return((Comparable) (((Networkpara) (O2)). Priority) . CompareTo (((Networkpara) (O1)). } }); String result=""; for(inti = 0; I < list.size (); i++) {result=Result.concat (List.get (i). parastring); } returnresult; }
2.3 Support for searching by string
The entry selection for string search uses the Add Search button on the Actionar to match the input string line by row. When there are multiple matching results, the forward and backward buttons are displayed in the interface to support the selection of front and back content.
2.3.1 Actionbar Display the search button
The search button is added first when the menu item is generated, and then the main activity implements Onquerytextlistener and implements the Onquerytextchange and Onquerytextsubmit methods [2].
public boolean Oncreateoptionsmenu (Menu menu) { // INF Late the menu; This adds items to the action bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); Searchview = (Searchview) Menuitemcompat.getactionview (Menu.finditem ( R.id.menu_search)); Searchview.setonquerytextlistener ( this ); return true ; }
<menu 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 " tools: Context= "Com.zhigao.all_connect." Mainactivity "> <item android:id=" @+id/menu_search " android:title=" Search " app: Showasaction= "Always" app:actionviewclass= "Android.support.v7.widget.SearchView" / ></menu>
2.3.2 string matching and result saving
After the user has entered the characters to be searched, click Search and execute the Onquerytextsubmit function. Use Stringsplit[i].tolowercase (). Contains (Arg0.tolowercase ()) for case-insensitive matching operations. Use the Scrollto function for ScrollView jumps [5].
Public Booleanonquerytextsubmit (String arg0) {//TODO auto-generated Method StubLOG.V (TAG, "querysubmit" +arg0); Matchedline.clear (); String []stringsplit=sortedresult.split ("\ n"); for(inti = 0; i < stringsplit.length; i++) { //Case insensitive match if(Stringsplit[i].tolowercase (). Contains (Arg0.tolowercase ())) {Matchedline.add (i); } } if(Matchedline.size () ==0) {Toast.maketext (Getapplicationcontext (),"No match!", Toast.length_short). Show (); return false; }Else if(Matchedline.size () ==1) { } Else{forwardbutton.setvisibility (view.visible); Backwardbutton.setvisibility (view.visible); } scrollview.post (NewRunnable () {@Override Public voidrun () {inty = Textview.getlayout (). Getlinetop (Matchedline.get (0)); Scrollview.scrollto (0, y); } }); Searchview.clearfocus (); return false; }
2.3.3 when there are multiple strings that can be matched when the result is displayed
The effect of button floating on TextView is constructed based on relativelayout, enabling the button to remain in the lower right position [6] When the user slides down ScrollView. The user taps the button for a forward or backward search operation. Click TextView to cancel the display of the button.
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" ><ScrollView Android:id= "@+id/scrollview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextView Android:id= "@+id/ssidtextview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </ScrollView> <Button Android:id= "@+id/backwardbutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:layout_alignparentright= "true"Android:background= "@drawable/backward"/> <Button Android:id= "@+id/forwardbutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_toleftof= "@id/backwardbutton"Android:layout_alignparentbottom= "true"Android:background= "@drawable/forward"/></relativelayout>
2.4 Problems encountered while generating a signed apk and the current solution 2.4.1 Duplicate ID @+id/image issue [7]
Generate signature apk run lint check, prompted by duplicate ID @+id/image issue. Even after updating the Android support library to 23.0.1, it still appears. The second @+id/image in Abc_activity_chooser_view.xml is now modified to @+id/image2.
2.4.2 This class should is public (Android.support.v7.internal.widget.ActionBarView.HomeView) issue
Modify the lint so that it will judge this problem from error to warning. Window->preferences, Android Lint preferences, search instantiatable. Set it to warning.
2.4.3 "Abc_action_bar_home_description_format" is not translated in "MK-RMK" issue
Because too many languages are currently not considered, and Android is likely to be updated after this package. Therefore, it is considered that the missing chanslation of lint is set to warning first.
Third, complete source sharing
Https://github.com/jue-jiang/wifiAssist
Four, apk download
https://github.com/jue-jiang/wifiAssist/blob/master/wifiAssist.apk
V. reference materials
[1] How to view WiFi password _ android phone Baidu experience
[2]searchview.setonquerytextlistener (this);
[3]java-android Reading from an Input stream efficiently-stack Overflow
[4]java-execute shell command from Android-stack Overflow
[5]java-how to scroll-a given line number, TextView inside Scrollview-stack Overflow
[6] How to add a floating button on scrolling in Android? -Stack Overflow
[7] Issue 73197-android-abc_activity_chooser_view_include.xml uses android:id= "@+id/image" twice-android Open Source Pro Ject-issue Tracker-google Project Hosting
Your Android phone has saved WiFi Password View assistant (open source)