码迷,mamicode.com
首页 > 移动开发 > 详细

操作手机wifi(打开、关闭、获取连接信息)

时间:2017-10-01 19:02:19      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:系统服务   列表   操作   mat   bundle   div   手机   vertica   tom   

页面布局:

<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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/start"
        android:layout_below="@+id/start"
        android:layout_marginTop="75dp"
        android:onClick="StateButton"
        android:text="state" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/state"
        android:layout_below="@+id/start"
        android:layout_marginTop="15dp"
        android:onClick="StopButton"
        android:text="stop" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="24dp"
        android:onClick="StartButton"
        android:text="start" />
    
     <Button
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="250dp"
        android:onClick="ListButton"
        android:text="getList" />

</RelativeLayout>

 

三个按钮:第一个打开wifi,第二个关闭wifi,第三个获取搜索到的wifi信息

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

activity:

public class MainActivity extends Activity {

    WifiManager wifiManage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wifiManage = (WifiManager) this.getSystemService(WIFI_SERVICE);//获取 wifi管理者
    }

    @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;
    }
/**
 * wifi管理者的各种方法可用于判断wifi状态、操控wifi打开和关闭
 * @param v
 */
    public void StartButton(View v){
        System.out.println("start");
        if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_DISABLED){
            wifiManage.setWifiEnabled(true);//打开wifi
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_DISABLING){
            System.out.println("正在关闭");
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_ENABLING){
            System.out.println("正在打开");
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_ENABLED){
            System.out.println("早已打开");
        }
    }
    public void StopButton(View v){
        System.out.println("stop");
        System.out.println("start");
        if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_DISABLED){
            System.out.println("早已关闭");
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_DISABLING){
            System.out.println("正在关闭");
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_ENABLING){
            System.out.println("正在打开");
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_ENABLED){
            wifiManage.setWifiEnabled(false);//关闭wifi
        }
    }
    public void StateButton(View v){
        System.out.println("state");
        System.out.println("start");
        if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_DISABLED){
            System.out.println(wifiManage.getWifiState());
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_DISABLING){
            System.out.println(wifiManage.getWifiState());
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_ENABLING){
            System.out.println(wifiManage.getWifiState());
        }else if(wifiManage.getWifiState()==wifiManage.WIFI_STATE_ENABLED){
            System.out.println(wifiManage.getWifiState());
        }
    }
    
    public void ListButton(View v) {
        //获取wifi列表的list
        List<WifiConfiguration> netWorkList = wifiManage.getConfiguredNetworks();
        //获取wifi信息
        System.out.println(netWorkList.get(0).SSID);
        System.out.println(netWorkList.get(1).SSID);
        System.out.println(netWorkList.get(2).SSID);
        System.out.println(netWorkList.get(3).SSID);
    }
}

 

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

总结:

1、wifi状态

  

wifiManage.WIFI_STATE_DISABLED//wifi不可用
wifiManage.WIFI_STATE_DISABLING//wifi正在关闭
wifiManage.WIFI_STATE_ENABLING//wifi正在打开
wifiManage.WIFI_STATE_ENABLED//wifi可用

wifiManage.getWifiState()//获取wifi当前状态

 

 

2、操作wifi

wifiManage.setWifiEnabled(false);//关闭wifi
wifiManage.setWifiEnabled(true);//打开wifi

 

 

3、获取wifi列表

List<WifiConfiguration> netWorkList =wifiManage.getConfiguredNetworks();

 

4、获取管理者

wifiManage = (WifiManager) this.getSystemService(WIFI_SERVICE);

 

即:获取系统服务----wifi管理者

 

操作手机wifi(打开、关闭、获取连接信息)

标签:系统服务   列表   操作   mat   bundle   div   手机   vertica   tom   

原文地址:http://www.cnblogs.com/sovagxa/p/7617562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!