标签:
1.WIFI网卡有一些状态,由一系列的整形常量来表示。
常量名 | 常量值 | 网卡状态 |
WIFI_STATE_DISABLING | 0 | 网卡正在关闭 |
WIFI_STATE_DISABLED | 1 | 网卡不可用 |
WIFI_STATE_ENABLING | 2 | 网卡正在打开 |
WIFI_STATE_ENABLED | 3 | 网卡可用 |
WIFI_STATE_UNKNOWN | 4 | 未知网卡状态 |
2.WIFI 的主要操作权限有四个:
CHANGE_NETWORK_STATE :允许修改网络状态的权限。
CHANGE_WIFI_STATE :允许修改 WIFI 状态的权限。
ACCESS_NETWORK_STATE :允许访问网络状态的权限。
ACCESS_WIFI_STATE :允许访问 WIFI 状态的权限。
在AndroidManifest.xml文件中添加权限。
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
3.改变WIFI网卡的状态
对WIFI网卡进行操作需要通过WifiManager对象来进行,获取该对象的方法如下:
WifiManager wifiManger=(WifiManger)Context.getSystemService(Service.WIFI-SERVICE);
打开WIFI网卡
wifiManager.setWifiEnabled(true);
关闭WIFI网卡
wifiManager.setWifiEnabled(false);
获取网卡当前的状态
wifiManager.getWifiState();
获取网卡的配置信息
wifiManager.getConfiguredNetworks();
如果要进入Android系统自带的WIFI配置界面,则用:
ACTION_WIFI_IP_SETTINGS WIFI的IP设置界面
ACTION_WIFI_SETTINGS WIFI设置界面
ACTION_WIRELESS_SETTINGS 网络设置界面
startActivityForResult(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS ), 0);
进入Android系统的设置界面:
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName("com.android.settings", "com.android.settings.Settings");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
4.操作WIFI例子
1 package com.ll.testwifi; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.content.Context; 5 import android.net.wifi.WifiManager; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.Toast; 13 14 15 public class MainActivity extends ActionBarActivity { 16 17 private Button startBtn = null; 18 private Button stopBtn = null; 19 private Button checkBtn = null; 20 private WifiManager wifiManager; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 startBtn = (Button)findViewById(R.id.start); 27 stopBtn = (Button)findViewById(R.id.stop); 28 checkBtn = (Button)findViewById(R.id.check); 29 // 添加消息响应 30 startBtn.setOnClickListener(new StartWifiListener()); 31 stopBtn.setOnClickListener(new StopWifiListener()); 32 checkBtn.setOnClickListener(new CheckWifiListener()); 33 34 } 35 36 37 @Override 38 public boolean onCreateOptionsMenu(Menu menu) { 39 // Inflate the menu; this adds items to the action bar if it is present. 40 getMenuInflater().inflate(R.menu.main, menu); 41 return true; 42 } 43 44 @Override 45 public boolean onOptionsItemSelected(MenuItem item) { 46 // Handle action bar item clicks here. The action bar will 47 // automatically handle clicks on the Home/Up button, so long 48 // as you specify a parent activity in AndroidManifest.xml. 49 int id = item.getItemId(); 50 if (id == R.id.action_settings) { 51 return true; 52 } 53 return super.onOptionsItemSelected(item); 54 } 55 56 // 开启WIFI 57 class StartWifiListener implements OnClickListener{ 58 59 @Override 60 public void onClick(View v) { 61 // TODO Auto-generated method stub 62 wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); 63 wifiManager.setWifiEnabled(true); 64 Toast.makeText(MainActivity.this, "当前WIFI状态为---->"+wifiManager.getWifiState(),Toast.LENGTH_SHORT).show(); 65 66 } 67 }// end of StartWifiListener 68 69 // 关闭WIFI 70 class StopWifiListener implements OnClickListener{ 71 72 @Override 73 public void onClick(View v) { 74 // TODO Auto-generated method stub 75 wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); 76 wifiManager.setWifiEnabled(false); 77 Toast.makeText(MainActivity.this, "当前WIFI状态为---->"+wifiManager.getWifiState(),Toast.LENGTH_SHORT).show(); 78 } 79 }// end of StopWifiListener 80 81 // 检查WIFI状态 82 class CheckWifiListener implements OnClickListener{ 83 84 @Override 85 public void onClick(View v) { 86 // TODO Auto-generated method stub 87 wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); 88 Toast.makeText(MainActivity.this, "当前WIFI状态为---->"+wifiManager.getWifiState(),Toast.LENGTH_SHORT).show(); 89 } 90 }// end of CheckWifiListener 91 }
标签:
原文地址:http://www.cnblogs.com/liul/p/4969273.html