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 @Override20 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 @Override36 public void onClick(View v) {37 // TODO Auto-generated method stub38 //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 @Override49 public void onClick(View v) {50 // TODO Auto-generated method stub51 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 @Override60 public void onClick(View v) {61 // TODO Auto-generated method stub62 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 @Override68 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、需要串連到手機做測試