您好,登錄后才能下訂單哦!
Wifi網卡狀態
WIFI_STATE_DISABLED Wifi網卡不可用
WIFI_STATE_DIABLING Wifi網卡正在關閉
WIFI_STATE_ENABLED Wifi網卡可用
WIFI_STATE_ENABLING Wifi網卡正在打開
WIFI_STATE_UNKNOWN 未知網卡狀態
操作Wifi網卡所需要的權限
CHANGE_NETWORK_STATE 改變網絡連接狀態
CHANGE_WIFI_STATE 改變Wifi連接狀態
ACCESS_NETWORK_STATE 允許應用獲取網絡狀態信息
ACCESS_WIFI_STATE 允許應用獲取Wifi狀態信息
界面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="啟動手機的wifi網卡" /> <Button android:id="@+id/stopWifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/startWifi" android:text="關閉手機的wifi網卡" /> <Button android:id="@+id/checkWifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/stopWifi" android:text="檢查當前wifi網卡狀態" />
需要的權限
<!-- 以下是使用wifi訪問網絡所需要的權限 --> <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, "當前Wifi網卡狀態為" + 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返回下面五個常量之一 下面的五個常量都是在WifiManager類中預定義的 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, "當前Wifi網卡狀態為" + 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, "當前Wifi網卡狀態為" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show(); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。