您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)android開發(fā)教程之如何實(shí)現(xiàn)wifi開發(fā)示例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
1、 WIFI網(wǎng)卡的狀態(tài)
WIFI網(wǎng)卡的狀態(tài)信息都以整型變量的形式存放在 android.net.wifi.WifiManager 類中,有以下狀態(tài):
WIFI_STATE_DISABLED
WIFI網(wǎng)卡不可用
WIFI_STATE_DISABLING
WIFI網(wǎng)卡正在關(guān)閉
WIFI_STATE_ENABLED
WIFI網(wǎng)卡可用
WIFI_STATE_ENABLING
WIFI網(wǎng)卡正在打開
WIFI_STATE_UNKNOWN
WIFI網(wǎng)卡狀態(tài)未知
2、 操作WIFI 網(wǎng)卡所需的權(quán)限
CHANGE_NETWORK_STATE
允許應(yīng)用程序改變網(wǎng)絡(luò)連接狀態(tài)
CHANGE_WIFI_STATE
允許應(yīng)用程序改變WIFI 連接狀態(tài)
ACCESS_NETWORK_STATE
允許應(yīng)用程序獲取網(wǎng)絡(luò)的狀態(tài)信息
ACCESS_WIFI_STATE
允許應(yīng)用程序獲得WIFI 的狀態(tài)信息
3、 改變WIFI 網(wǎng)卡的狀態(tài)
對WIFI 網(wǎng)卡進(jìn)行操作需要通過對 WifiManager 對象來進(jìn)行,獲取該對象的方法如下:
WifiManager wifiManager=(WifiManager) Context.getSystemService(Service.WIFI_SERVICE);
打開WIFI 網(wǎng)卡,代碼如下:
wifiManager.setWifiEnabled(true);
關(guān)閉WIFI 網(wǎng)卡,代碼如下:
wifiManager.setWifiEnabled(false);
獲取網(wǎng)卡的當(dāng)前狀態(tài),代碼如下:
wifiManager.getWifiState();
示例代碼:由于Android模擬器不支持wifi操作,所以例子需要在實(shí)體手機(jī)上運(yùn)行
AndroidManifest.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.bym" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WifiActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<!-- 以下是使用wifi訪問網(wǎng)絡(luò)所需要的權(quán)限 -->
<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>
</manifest>
Activity,代碼如下:
package me.bym;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class WifiActivity extends Activity {
private Button startWifiButton = null;
private Button shutdownWifiButton = null;
private Button checkStateButton = null;
private WifiManager wifiManager = null;
private String res = ""; // 用來存放操作結(jié)果信息的字符串
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startWifiButton = (Button) findViewById(R.id.StartWifiButton);
shutdownWifiButton = (Button) findViewById(R.id.ShutdownWifiButton);
checkStateButton = (Button) findViewById(R.id.CheckStateButton);
startWifiButton.setOnClickListener(new StartWifiListener());
shutdownWifiButton.setOnClickListener(new ShutdownWifiListener());
checkStateButton.setOnClickListener(new CheckStateListener());
}
class StartWifiListener implements OnClickListener {
@Override
public void onClick(View v) {
wifiManager = (WifiManager) WifiActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.setWifiEnabled(true)) {
res = "啟動(dòng)wifi服務(wù)成功";
} else {
res = "啟動(dòng)wifi服務(wù)失敗";
}
Toast.makeText(WifiActivity.this, res, Toast.LENGTH_SHORT).show();
}
}
class ShutdownWifiListener implements OnClickListener {
@Override
public void onClick(View v) {
wifiManager = (WifiManager) WifiActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.setWifiEnabled(false)) {
res = "關(guān)閉wifi服務(wù)成功";
} else {
res = "關(guān)閉wifi服務(wù)失敗";
}
Toast.makeText(WifiActivity.this, res, Toast.LENGTH_SHORT).show();
}
}
class CheckStateListener implements OnClickListener {
@Override
public void onClick(View v) {
wifiManager = (WifiManager) WifiActivity.this
.getSystemService(Context.WIFI_SERVICE);
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_DISABLED:
res = "WIFI已關(guān)閉";
break;
case WifiManager.WIFI_STATE_DISABLING:
res = "WIFI正在關(guān)閉中";
break;
case WifiManager.WIFI_STATE_ENABLED:
res = "WIFI已啟用";
break;
case WifiManager.WIFI_STATE_ENABLING:
res = "WIFI正在啟動(dòng)中";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
res = "未知WIFI狀態(tài)";
break;
}
Toast.makeText(WifiActivity.this, res, Toast.LENGTH_SHORT).show();
}
}
}
效果圖:
關(guān)于android開發(fā)教程之如何實(shí)現(xiàn)wifi開發(fā)示例就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。