Android学习笔记-Wifi网络操作
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(); } } }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1582680
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。