Android 学习 笔记_09. WIFI网络操作
1、概念
2、wifi网卡的状态
WIFI_STATE_DISABLED:wifi网卡不可用(1)
WIFI_STATE_DISABLING:wifi正在关闭(0)
WIFI_STATE_ENABLED:wifi网卡可用(3)
WIFI_STATE_ENABLING:wifi网卡正在打开(2)
WIFI_STATE_UNKNOWN:未知网卡状态
3、wifi的权限
(部分与网络有关的权限,具体的其他权限可以查阅帮助文档)
4、改变wifi网卡的状态
对wifi网卡的的操作需要对WifiManager对象进行,方法如下:
WifiManager wifiManager=(WifiManager)Context.getSystemService(Service.WIFI_SERVICE);
打开网卡:wifiManager.setWifiEnabled(true);
关闭wifi网卡:wifiManager.setWifiEnabled(false);
获取当前网卡状态:wifimanager.getWifiState();
现实现一个访问wifi的程序,需要有一台安卓系统的手机做测试,具体内容是启动、停止、查看wifi网络。
WifiActivity.java
1 package zzl.wifi; 2 3 import android.app.Activity; 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.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.Toast; 12 13 public class WifiActivity extends Activity { 14 15 private Button start=null; 16 private Button stop=null; 17 private Button check=null; 18 private WifiManager wifiManager=null; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 24 start=(Button)findViewById(R.id.start); 25 stop=(Button)findViewById(R.id.stop); 26 check=(Button)findViewById(R.id.check); 27 28 start.setOnClickListener(new StartWifiListener()); 29 stop.setOnClickListener(new StopWifiListener()); 30 check.setOnClickListener(new CheckWifiListener()); 31 } 32 33 class StartWifiListener implements OnClickListener{ 34 35 @Override 36 public void onClick(View v) { 37 // TODO Auto-generated method stub 38 //WifiActivity继承自Activity, 39 //Activity本身又是Context的子类(所以Context的所有函数Activity都可以用) 40 wifiManager=(WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE); 41 wifiManager.setWifiEnabled(true); 42 System.out.println("wiif state--->"+wifiManager.getWifiState()); 43 Toast.makeText(WifiActivity.this, "当前wifi状态:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); 44 } 45 } 46 class StopWifiListener implements OnClickListener{ 47 48 @Override 49 public void onClick(View v) { 50 // TODO Auto-generated method stub 51 wifiManager=(WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE); 52 wifiManager.setWifiEnabled(false); 53 System.out.println("wiif state--->"+wifiManager.getWifiState()); 54 Toast.makeText(WifiActivity.this, "当前wifi状态:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); 55 } 56 } 57 class CheckWifiListener implements OnClickListener{ 58 59 @Override 60 public void onClick(View v) { 61 // TODO Auto-generated method stub 62 wifiManager=(WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE); 63 System.out.println("wiif state--->"+wifiManager.getWifiState()); 64 Toast.makeText(WifiActivity.this, "当前wifi状态:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); 65 } 66 } 67 @Override 68 public boolean onCreateOptionsMenu(Menu menu) { 69 // Inflate the menu; this adds items to the action bar if it is present. 70 getMenuInflater().inflate(R.menu.main, menu); 71 return true; 72 } 73 74 }
main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".WifiActivity" > 7 8 <Button 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:id="@+id/start" 12 android:text="@string/start"/> 13 <Button 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:id="@+id/stop" 17 android:text="@string/stop"/> 18 <Button 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/check" 22 android:text="@string/check"/> 23 24 </LinearLayout>
部分结果:
总结:
(1、 在Manifest.xml中出现这样的 <uses-permission> tag appears after <application> tag 警告
解决方法是:把<uses-permission> 放到 <application> 前面
(2、要使wifi访问网络,需要在Manifest.xml中敲写这样的代码,否则可能会造成访问权限不足
1 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission> 2 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 3 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 4 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
(3、需要连接到手机做测试