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(); } } }
时间: 2024-11-05 18:28:31