android WIFI网络操作

1、wifi网卡有以下几种状态:

WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);

switch(mWiFiManager01.getWifiState())
     {
       
       caseWifiManager.WIFI_STATE_ENABLED:
         mTextView01.setText
         (
           getResources().getText(R.string.str_wifi_enabling)
         );
         break;
       
       caseWifiManager.WIFI_STATE_ENABLING:
         mTextView01.setText
         (
           getResources().getText(R.string.str_wifi_enabling)
         );
         break;
       
       caseWifiManager.WIFI_STATE_DISABLING:
         mTextView01.setText
         (
           getResources().getText(R.string.str_wifi_disabling)
         );
         break;
       
       caseWifiManager.WIFI_STATE_DISABLED:
         mTextView01.setText
         (
           getResources().getText(R.string.str_wifi_disabled)
         );
         break;
       
       caseWifiManager.WIFI_STATE_UNKNOWN:
       default:
         mTextView01.setText
         (
           getResources().getText(R.string.str_wifi_unknow)
         );
         break;
     }


2、 要对wifi进行操作,首先要在AndroidMenifest.xml中取得权限的,如下:

<!-- 以下是使用wifi访问网络所需要的权限-->

<uses-permissionAndroid:name="android.permission.CHANGE_NETWORK_STATE">


</uses-permission>  //修改网络状态的权限

<uses-permissionAndroid:name="android.permission.CHANGE_WIFI_STATE">

</uses-permission>  //修改wifi状态的权限

<uses-permissionAndroid:name="android.permission.ACCESS_NETWORK_STATE">

</uses-permission>  //访问网络权限

<uses-permissionAndroid:name="android.permission.ACCESS_WIFI_STATE">

</uses-permission>   //访问wifi权限

3、  对wifi网卡进行操作需要通过WifiManger对象来进行,获取该对象的方法如下:

WifiManger wifiManger =

(WifiManger)Context.getSystemService(Service.WIFI_SERVICE);

打开wifi网卡

Wifimanger.setWifiEnabled(true);

关闭wifi网卡

wifiManger.setWifiEnablee(false);

获取网卡的当前的状态

wifiManger.getWifiState();



主要代码如下:

package mars.wifi;

import Android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class WifiActivity extends Activity {
    /** Called when the activity is first created. */
 private Button startButton = null;
 private Button stopButton = null;
 private Button checkButton = null;
 private WifiManager wifiManager = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = (Button)findViewById(R.id.startWifi);
        stopButton = (Button)findViewById(R.id.stopWifi);
        checkButton = (Button)findViewById(R.id.checkWifi);
        startButton.setOnClickListener(new StartWifiListener());
        stopButton.setOnClickListener(new StopWifiListener());
        checkButton.setOnClickListener(new CheckWifiListener());
    }
    class StartWifiListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
   wifiManager.setWifiEnabled(true);
   Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + 
wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
  }
    }
    class StopWifiListener implements OnClickListener{

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
   wifiManager.setWifiEnabled(false);
   Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + 
wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
  }
    
    }
    
    class CheckWifiListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
   Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + 
wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
  }
     
    }
}



你可能感兴趣的:(android WIFI网络操作)