操作手机wifi(打开关闭获取连接信息)
Posted sovagxa&静默
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作手机wifi(打开关闭获取连接信息)相关的知识,希望对你有一定的参考价值。
页面布局:
<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(打开关闭获取连接信息)的主要内容,如果未能解决你的问题,请参考以下文章