您好,登錄后才能下訂單哦!
Wifi網(wǎng)卡狀態(tài)
WIFI_STATE_DISABLED Wifi網(wǎng)卡不可用
WIFI_STATE_DIABLING Wifi網(wǎng)卡正在關(guān)閉
WIFI_STATE_ENABLED Wifi網(wǎng)卡可用
WIFI_STATE_ENABLING Wifi網(wǎng)卡正在打開
WIFI_STATE_UNKNOWN 未知網(wǎng)卡狀態(tài)
操作Wifi網(wǎng)卡所需要的權(quán)限
CHANGE_NETWORK_STATE 改變網(wǎng)絡(luò)連接狀態(tài)
CHANGE_WIFI_STATE 改變Wifi連接狀態(tài)
ACCESS_NETWORK_STATE 允許應(yīng)用獲取網(wǎng)絡(luò)狀態(tài)信息
ACCESS_WIFI_STATE 允許應(yīng)用獲取Wifi狀態(tài)信息
界面activity_main.xml
<TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/startWifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/hello" android:text="啟動(dòng)手機(jī)的wifi網(wǎng)卡" /> <Button android:id="@+id/stopWifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/startWifi" android:text="關(guān)閉手機(jī)的wifi網(wǎng)卡" /> <Button android:id="@+id/checkWifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/stopWifi" android:text="檢查當(dāng)前wifi網(wǎng)卡狀態(tài)" />
需要的權(quán)限
<!-- 以下是使用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>
MainActivity.java
public class MainActivity extends Activity { private Button startButton = null; private Button stopButton = null; private Button checkButton = null; private WifiManager wifiManager = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button)findViewById(R.id.startWifi); stopButton = (Button)findViewById(R.id.stopWifi); checkButton = (Button)findViewById(R.id.checkWifi); startButton.setOnClickListener(new StartWifiListener()); stopButton.setOnClickListener(new StopWifiListener()); checkButton.setOnClickListener(new CheckWifiListener()); } class StartWifiListener implements OnClickListener{ @Override public void onClick(View v) { wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true); System.out.println("wifi state --->" + wifiManager.getWifiState()); Toast.makeText(MainActivity.this, "當(dāng)前Wifi網(wǎng)卡狀態(tài)為" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); } } class StopWifiListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(false); System.out.println("wifi state --->" + wifiManager.getWifiState()); /* getWifiState返回下面五個(gè)常量之一 下面的五個(gè)常量都是在WifiManager類中預(yù)定義的 public static final int WIFI_STATE_DISABLING = 0; public static final int WIFI_STATE_DISABLED = 1; public static final int WIFI_STATE_ENABLING = 2; public static final int WIFI_STATE_ENABLED = 3; public static final int WIFI_STATE_UNKNOWN = 4; */ Toast.makeText(MainActivity.this, "當(dāng)前Wifi網(wǎng)卡狀態(tài)為" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); } } class CheckWifiListener implements OnClickListener{ @Override public void onClick(View v) { wifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); System.out.println("wifi state --->" + wifiManager.getWifiState()); Toast.makeText(MainActivity.this, "當(dāng)前Wifi網(wǎng)卡狀態(tài)為" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); } } }
免責(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)容。