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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值