android控制wifi状态
wifi的五种状态
常量名 | 常量值 | 网卡状态 |
WIFI_STATE_DISABLED | 1 | WIFI网卡不可用 |
WIFI_STATE_DISABLING | 0 | WIFI正在关闭 |
WIFI_STATE_ENABLED | 3 | WIFI网卡可用 |
WIFI_STATE_ENABLING | 2 | WIFI网卡正在打开 |
WIFI_STATE_UNKNOWN | 4 | 未知网卡状态 |
public class MainActivity extends Activity { WifiManager mWifiManager = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { switch (v.getId()) { case R.id.open_wifi: mWifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); mWifiManager.setWifiEnabled(true); if(mWifiManager.getWifiState() == 2) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI正在打开...", Toast.LENGTH_SHORT).show(); } else if(mWifiManager.getWifiState() == 3) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI正常使用", Toast.LENGTH_SHORT).show(); } break; case R.id.close_wifi: mWifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); mWifiManager.setWifiEnabled(false); if(mWifiManager.getWifiState() == 0) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI正在关闭...", Toast.LENGTH_SHORT).show(); } else if(mWifiManager.getWifiState() == 1) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI已关闭", Toast.LENGTH_SHORT).show(); } break; case R.id.wifi_state: mWifiManager = (WifiManager)MainActivity.this.getSystemService(Context.WIFI_SERVICE); if(mWifiManager.getWifiState() == 0) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI正在关闭...", Toast.LENGTH_SHORT).show(); } else if(mWifiManager.getWifiState() == 1) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI已关闭", Toast.LENGTH_SHORT).show(); } else if(mWifiManager.getWifiState() == 2) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI正在打开...", Toast.LENGTH_SHORT).show(); } else if(mWifiManager.getWifiState() == 3) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI正常使用", Toast.LENGTH_SHORT).show(); } else if(mWifiManager.getWifiState() == 4) { Toast.makeText(MainActivity.this, "当前网卡状态为:WIFI状态未知", Toast.LENGTH_SHORT).show(); } break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/open_wifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="打开WIFI" android:onClick="onClick" /> <Button android:id="@+id/close_wifi" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="关闭WIFI" android:onClick="onClick"/> <Button android:id="@+id/wifi_state" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="WIFI状态" android:onClick="onClick"/> </LinearLayout>
添加权限:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> CHANGE_NETWORK_STATE :允许修改网络状态的权限。 CHANGE_WIFI_STATE :允许修改 WIFI 状态的权限。 ACCESS_NETWORK_STATE :允许访问网络状态的权限。 ACCESS_WIFI_STATE :允许访问 WIFI 状态的权限。
移动开发qq群:59516399