android Wifi 控制

android 当中 wifi 控制非常简单。

在android 系统当中 wifi 有五种状态:

  
  // Wi-Fi AP is currently being disabled. The state will change to
    public static final int WIFI_AP_STATE_DISABLING = 0;
   // Wi-Fi AP is disabled.
    public static final int WIFI_AP_STATE_DISABLED = 1;
   // Wi-Fi AP is currently being enabled. The state will change to
    public static final int WIFI_AP_STATE_ENABLING = 2;
   // Wi-Fi AP is enabled.
    public static final int WIFI_AP_STATE_ENABLED = 3;
   // Wi-Fi AP is in a failed state. This state will occur when an error occurs during
    public static final int WIFI_AP_STATE_FAILED = 4;

可以通过系统调用去改变其状态进而控制wifi

实例效果如下:点击按钮来改变 wifi 状态,并用 Toast 打印出当前的 wifi 状态


 


代码如下

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/openWIFI"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="openWIFI" />

    <Button
        android:id="@+id/closeWIFI"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="closeWIFI" />

    <Button
        android:id="@+id/checkState"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="checkState" />

</LinearLayout>


wifiActivity.java

package cie.WIFI;

import cie.WIFI.R;
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.openWIFI);
        stopButton = (Button)findViewById(R.id.closeWIFI);
        checkButton = (Button)findViewById(R.id.checkState);
        
        startButton.setOnClickListener(new StartWifiListener());
        stopButton.setOnClickListener(new StopWifiListener());
        checkButton.setOnClickListener(new CheckWifiListener());
    }
	
	class StartWifiListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			wifiManager = (WifiManager)WIFIActivity.this.getSystemService(Context.WIFI_SERVICE);
			wifiManager.setWifiEnabled(true);
			System.out.println("wifi state ----" + wifiManager.getWifiState());
			Toast toast = Toast.makeText(WIFIActivity.this, "当前网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
			toast.show();
		}

	}

	class StopWifiListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			wifiManager = (WifiManager)WIFIActivity.this.getSystemService(Context.WIFI_SERVICE);
			wifiManager.setWifiEnabled(false);
			System.out.println("wifi state ----" + wifiManager.getWifiState());
			Toast toast = Toast.makeText(WIFIActivity.this, "当前网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
			toast.show();
		}

	}

	class CheckWifiListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			wifiManager = (WifiManager)WIFIActivity.this.getSystemService(Context.WIFI_SERVICE);
			System.out.println("wifi state ----" + wifiManager.getWifiState());
			Toast toast = Toast.makeText(WIFIActivity.this, "当前网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
			toast.show();
		}

	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值