Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android管理WIFI

Android管理WIFI

編輯:關於Android編程

首先,需要權限

[html]
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 

   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
WIF的狀態在WifiManager中定義了常量,都是public static final int類型的,如下

[java]
public static final int WIFI_STATE_DISABLED 
Wi-Fi is disabled. 
Constant Value: 1 (0x00000001) 
 
 
public static final int WIFI_STATE_DISABLING 
Wi-Fi is currently being disabled. The state will change to WIFI_STATE_DISABLED if it finishes successfully. 
Constant Value: 0 (0x00000000) 
 
 
public static final int WIFI_STATE_ENABLED 
Wi-Fi is enabled. 
Constant Value: 3 (0x00000003) 
 
 
 
 
public static final int WIFI_STATE_ENABLING 
Wi-Fi is currently being enabled. The state will change to WIFI_STATE_ENABLED if it finishes successfully. 
Constant Value: 2 (0x00000002) 
 
 
 
public static final int WIFI_STATE_UNKNOWN 
Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling. 
Constant Value: 4 (0x00000004) 

public static final int WIFI_STATE_DISABLED
Wi-Fi is disabled.
Constant Value: 1 (0x00000001)


public static final int WIFI_STATE_DISABLING
Wi-Fi is currently being disabled. The state will change to WIFI_STATE_DISABLED if it finishes successfully.
Constant Value: 0 (0x00000000)


public static final int WIFI_STATE_ENABLED
Wi-Fi is enabled.
Constant Value: 3 (0x00000003)

 


public static final int WIFI_STATE_ENABLING
Wi-Fi is currently being enabled. The state will change to WIFI_STATE_ENABLED if it finishes successfully.
Constant Value: 2 (0x00000002)

 

public static final int WIFI_STATE_UNKNOWN
Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling.
Constant Value: 4 (0x00000004)

定義一個並且初始化WifiManager manager==(WifiManager) Context.getSystemService(Context.WIFI_SERVICE);

啟動WIFI manager.setWifiEnabled(true);

關閉WIFI manager.setWifiEnabled(false);

獲取WIFI狀態 manager.getWifiState();返回前面說的五種狀態的一種

下面是全部代碼

[java]
package com.zhycheng.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 WIFIDemoActivity extends Activity implements OnClickListener { 
    Button start,stop,state; 
    WifiManager manager; 
     
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        start=(Button) findViewById(R.id.startWIFI); 
        stop=(Button) findViewById(R.id.stopWIFI); 
        state=(Button) findViewById(R.id.getSTATE); 
        start.setOnClickListener(this); 
        stop.setOnClickListener(this); 
        state.setOnClickListener(this); 
    } 
 
 
 
    @Override 
    public void onClick(View v) { 
        manager=(WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
         
        switch(v.getId()) 
        { 
        case R.id.startWIFI: 
            manager.setWifiEnabled(true); 
            System.out.println("當前WIFI狀態為"+manager.getWifiState()); 
            Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show(); 
             
            break; 
        case R.id.stopWIFI: 
            manager.setWifiEnabled(false); 
            System.out.println("當前WIFI狀態為"+manager.getWifiState()); 
            Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show(); 
             
            break; 
        case R.id.getSTATE: 
             
            System.out.println("當前WIFI狀態為"+manager.getWifiState()); 
            Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show(); 
            break; 
        } 
         
    } 

package com.zhycheng.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 WIFIDemoActivity extends Activity implements OnClickListener {
 Button start,stop,state;
 WifiManager manager;
 
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start=(Button) findViewById(R.id.startWIFI);
        stop=(Button) findViewById(R.id.stopWIFI);
        state=(Button) findViewById(R.id.getSTATE);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);
        state.setOnClickListener(this);
    }

 

 @Override
 public void onClick(View v) {
  manager=(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  
  switch(v.getId())
  {
  case R.id.startWIFI:
   manager.setWifiEnabled(true);
   System.out.println("當前WIFI狀態為"+manager.getWifiState());
   Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
   
   break;
  case R.id.stopWIFI:
   manager.setWifiEnabled(false);
   System.out.println("當前WIFI狀態為"+manager.getWifiState());
   Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
   
   break;
  case R.id.getSTATE:
   
   System.out.println("當前WIFI狀態為"+manager.getWifiState());
   Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
   break;
  }
  
 }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved