WIFI Demo

package com.example.testwififun;

import java.util.List;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WifiActivity extends Activity {
 
 //变量定义
  private TextView allNetWork;   
     private Button scan;   
  private Button start;   
     private Button stop;   
  private Button check;  
  private WifiManager mWifiManager; 
 
  //扫描结果列表
  private List<ScanResult> list;
  private ScanResult mScanResult;
  private StringBuffer sb = new StringBuffer();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_wifi);
  
  //添加事件响应
  scan=(Button)findViewById(R.id.scan);
  start=(Button)findViewById(R.id.start);
  stop=(Button)findViewById(R.id.stop);
  check=(Button)findViewById(R.id.check);
  allNetWork=(TextView)findViewById(R.id.allNetWork);
  mWifiManager=(WifiManager)WifiActivity.this.getSystemService(WIFI_SERVICE);
  
  scan.setOnClickListener(new MyListener());
  start.setOnClickListener(new MyListener());
  stop.setOnClickListener(new MyListener());
  check.setOnClickListener(new MyListener());
 }

 public class MyListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   switch(v.getId())
   {
   case R.id.scan: //扫描网络
    getAllNetworkList();
    break;
    
   case R.id.start: //打开网络
    if(!mWifiManager.isWifiEnabled())
    {
     mWifiManager.setWifiEnabled(true);
     Toast.makeText(WifiActivity.this,"======  WIFI已打开  ======", 1).show();
     //allNetWork.setText("");
     //getAllNetworkList();
    }
    else
    {
     Toast.makeText(WifiActivity.this,"======  WIFI已打开  ======", 1).show();
     //getAllNetworkList();
    }
    break;
    
   case R.id.stop: //关闭网络
     mWifiManager.setWifiEnabled(false);
    // if(mWifiManager.getWifiState()==1)
     Toast.makeText(WifiActivity.this,"======  WIFI已关闭  ======", 1).show(); 
     allNetWork.setText("");
    break;
    
   case R.id.check: //检查网络
    Toast.makeText(WifiActivity.this, "当前WIFI状态为:"+mWifiManager.getWifiState(), 1).show(); 
   default:
     break;
   }
  }
  
 }
 
 public void getAllNetworkList()
 {
  //每次扫描前都清空上一次的扫描结果
  if(!mWifiManager.isWifiEnabled())
  {
   Toast.makeText(WifiActivity.this,"======  WIFI未打开,请打开WIFI  ======", 1).show(); 
  }
  if(sb!=null)
   sb = new StringBuffer();
  //allNetWork.setText("");
  
  //开始扫描网络
  mWifiManager.startScan();
  //得到扫描结果
  list = mWifiManager.getScanResults();
  //得到扫描结果
  if(list!=null)
  {
   for(int i=0;i<list.size();i++)
   {
    //得到扫描结果
    mScanResult = list.get(i);
    sb=sb.append("【"+(i+1)+"】"+"  ").append(mScanResult.SSID+"  ").append(mScanResult.capabilities+"  ").append(mScanResult.frequency+"  ").append(mScanResult.level+"\n\n");
   }
   allNetWork.setText("扫描到的WIFI网络:\n"+sb.toString());
  }
 }

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

}

 

========================================================================

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
  >

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
   
<Button   
    android:id="@+id/scan"   
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:text="扫描网络"   
   /> 
 
<Button   
    android:id="@+id/start"   
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:text="打开WIFI"   
   />   

<Button   
    android:id="@+id/stop"   
    android:layout_width="fill_parent"   
   android:layout_height="wrap_content"    
    android:text="关闭WIFI"   
    />   

<Button   
    android:id="@+id/check"   
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:text="WIFI状态"   
    />   

 <TextView   
    android:id="@+id/allNetWork"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"    
    android:text="当前没有扫描到WIFI网络"   
   />  
</LinearLayout>
</ScrollView>

========================================================================

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

你可能感兴趣的:(wifi)