1. 程式人生 > >android 獲取wifi 密碼 程式碼

android 獲取wifi 密碼 程式碼

 想要獲取wifi密碼首先要保證你的手機已經root、 因為只有root 以後才能讀取存放密碼的配置檔案

    如果你的手機已經root 用該語句獲取 使你的程式獲取許可權           Process process = Runtime.getRuntime().exec("su");

  下面是  程式的佈局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="152dp"
        android:text="獲取密碼" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="101dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="71dp"
        android:text= "退出"/>

</RelativeLayout>

下面是程式的程式碼

package com.android.getpsk;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button myButton;
	Button quit;
	TextView show;
	MyHandler myHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myButton = (Button) findViewById(R.id.button1);
		quit = (Button) findViewById(R.id.button2);
		show  = (TextView)findViewById(R.id.textView1);
		myHandler = new MyHandler();
		myButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v)
        	{      		
        		Log.e("Fuck", "I got a key down");   	
        		MyThread m = new MyThread();
                new Thread(m).start();
        	}
        });
		quit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
			
		});
	}
	
	 @SuppressLint("HandlerLeak")
	class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			Bundle b = msg.getData();
            String info = b.getString("info");
            MainActivity.this.show.setText(info);
            
		}
		 
	 }
	
	
	class MyThread implements Runnable {
        public void run() {
           ;
            StringBuilder content = new StringBuilder();
			try {
			 Process 	process = Runtime.getRuntime().exec("su");			
				String cmd = "cat /data/misc/wifi/wpa_supplicant.conf";
				//String cmd = "id";
				DataOutputStream dataOutputStream = new DataOutputStream(process.getOutputStream());
				DataInputStream dataIntputStream = new DataInputStream(process.getInputStream());
				DataInputStream dataErrorStream = new DataInputStream(process.getErrorStream());
				dataOutputStream.writeBytes(cmd + "\n");			
				dataOutputStream.flush();
				Thread.sleep(2000);
				
				String line = "";
				if (dataIntputStream.available() > 0)
				{
					String error = "";
					
					int total = dataIntputStream.available();
					Log.e("TotalCount", Integer.toString(total));
					int i = 0;
					while(i < total)
					{	
						
						line = dataIntputStream.readLine();
						if(line.trim().startsWith("ssid=") || line.trim().startsWith("psk="))
						{
							content.append(line + "\n");
						}
						
						i += line.length() + 1;
					}
					
					dataOutputStream.close();
					dataErrorStream.close();
					dataErrorStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("Exception1", e.toString());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("Exception2", e.toString());
			}
                      
            Message msg = new Message();
            Bundle b = new Bundle();// 存放資料
            b.putString("info", content.toString());
            Log.e("info", content.toString());
            msg.setData(b);
            MainActivity.this.myHandler.sendMessage(msg); // 向Handler傳送訊息,更新UI
        }
    }



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


執行結果我就不截圖了。我親測是Ok 的  

轉載自http://download.csdn.net/detail/chw_611/5275223