編輯:關於Android編程
首先,需要權限
[html]
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
WIF的狀態在WifiManager中定義了常量,都是public static final int類型的,如下
[java]
public static final int WIFI_STATE_DISABLED
Wi-Fi is disabled.
Constant Value: 1 (0x00000001)
public static final int WIFI_STATE_DISABLING
Wi-Fi is currently being disabled. The state will change to WIFI_STATE_DISABLED if it finishes successfully.
Constant Value: 0 (0x00000000)
public static final int WIFI_STATE_ENABLED
Wi-Fi is enabled.
Constant Value: 3 (0x00000003)
public static final int WIFI_STATE_ENABLING
Wi-Fi is currently being enabled. The state will change to WIFI_STATE_ENABLED if it finishes successfully.
Constant Value: 2 (0x00000002)
public static final int WIFI_STATE_UNKNOWN
Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling.
Constant Value: 4 (0x00000004)
public static final int WIFI_STATE_DISABLED
Wi-Fi is disabled.
Constant Value: 1 (0x00000001)
public static final int WIFI_STATE_DISABLING
Wi-Fi is currently being disabled. The state will change to WIFI_STATE_DISABLED if it finishes successfully.
Constant Value: 0 (0x00000000)
public static final int WIFI_STATE_ENABLED
Wi-Fi is enabled.
Constant Value: 3 (0x00000003)
public static final int WIFI_STATE_ENABLING
Wi-Fi is currently being enabled. The state will change to WIFI_STATE_ENABLED if it finishes successfully.
Constant Value: 2 (0x00000002)
public static final int WIFI_STATE_UNKNOWN
Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling.
Constant Value: 4 (0x00000004)
定義一個並且初始化WifiManager manager==(WifiManager) Context.getSystemService(Context.WIFI_SERVICE);
啟動WIFI manager.setWifiEnabled(true);
關閉WIFI manager.setWifiEnabled(false);
獲取WIFI狀態 manager.getWifiState();返回前面說的五種狀態的一種
下面是全部代碼
[java]
package com.zhycheng.wifi;
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 WIFIDemoActivity extends Activity implements OnClickListener {
Button start,stop,state;
WifiManager manager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button) findViewById(R.id.startWIFI);
stop=(Button) findViewById(R.id.stopWIFI);
state=(Button) findViewById(R.id.getSTATE);
start.setOnClickListener(this);
stop.setOnClickListener(this);
state.setOnClickListener(this);
}
@Override
public void onClick(View v) {
manager=(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
switch(v.getId())
{
case R.id.startWIFI:
manager.setWifiEnabled(true);
System.out.println("當前WIFI狀態為"+manager.getWifiState());
Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
break;
case R.id.stopWIFI:
manager.setWifiEnabled(false);
System.out.println("當前WIFI狀態為"+manager.getWifiState());
Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
break;
case R.id.getSTATE:
System.out.println("當前WIFI狀態為"+manager.getWifiState());
Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
break;
}
}
}
package com.zhycheng.wifi;
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 WIFIDemoActivity extends Activity implements OnClickListener {
Button start,stop,state;
WifiManager manager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button) findViewById(R.id.startWIFI);
stop=(Button) findViewById(R.id.stopWIFI);
state=(Button) findViewById(R.id.getSTATE);
start.setOnClickListener(this);
stop.setOnClickListener(this);
state.setOnClickListener(this);
}
@Override
public void onClick(View v) {
manager=(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
switch(v.getId())
{
case R.id.startWIFI:
manager.setWifiEnabled(true);
System.out.println("當前WIFI狀態為"+manager.getWifiState());
Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
break;
case R.id.stopWIFI:
manager.setWifiEnabled(false);
System.out.println("當前WIFI狀態為"+manager.getWifiState());
Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
break;
case R.id.getSTATE:
System.out.println("當前WIFI狀態為"+manager.getWifiState());
Toast.makeText(this, "當前WIFI狀態為"+manager.getWifiState(), Toast.LENGTH_SHORT).show();
break;
}
}
}
本文實例講述了Android RadioGroup和RadioButton控件簡單用法。分享給大家供大家參考,具體如下:RadioGroup和RadioButton代表的
針對app線上修復技術,目前有好幾種解決方案,開源界往往一個方案會有好幾種實現。重復的實現會有造輪子之嫌,但分析解決方案在技術上的探索和衍變,這輪子還是值得去推動的關於
Android 6.0 變化 Android 6.0 ChangesIn this document運行時權限Runtime Perm
知識點今天繼續昨天沒有講完的Menu的學習,主要是Popup Menu的學習。Popup Menu(彈出式菜單)彈出式菜單是一種固定在View上的菜單模型。主要用於以下三