博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android WIFI检测与设置
阅读量:6940 次
发布时间:2019-06-27

本文共 9897 字,大约阅读时间需要 32 分钟。

WIFI是无线通信协议,可以允许手机直接连接到无线网络。在现在3G资费还比较贵的情况下,WIFI对于手机来说是很重要的,我们可以很方便的下载软件,音乐等资源。Android手机必须要有WIFI网卡才能支持WIFI。Android应用程序有时候需要对WIFI网卡进行操作,从而操作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 未知网卡状态

在应用程序中操作WIFI网卡一定的权限。

     WIFI 的主要操作权限有四个:
              CHANGE_NETWORK_STATE :允许修改网络状态的权限。
              CHANGE_WIFI_STATE :允许修改 WIFI 状态的权限。
              ACCESS_NETWORK_STATE :允许访问网络状态的权限。
              ACCESS_WIFI_STATE :允许访问 WIFI 状态的权限。
在AndroidManifest.xml文件中添加权限。

    <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>
改变WIFI网卡的状态
     对WIFI网卡进行操作需要通过WifiManager对象来进行,获取该对象的方法如下:
      WifiManager wifiManger=(WifiManger)Context.getSystemService(Service.WIFI-SERVICE);
      打开WIFI网卡
      wifiManager.setWifiEnabled(true);
      关闭WIFI网卡
      wifiManager.setWifiEnabled(false);
      获取网卡当前的状态
      wifiManager.getWifiState();

示例:新建一个一个Android应用程序,在main.xml中添加三个按钮,点击这三个按钮分别可以打开WIFI网卡,关闭WIFI网卡,检查网卡  的当前状态。需要说明的是由于Android模拟器不支持WIFI和蓝牙所以程序执行时返回的网卡状态都是WIFI_STATE_UNKNOWN:网卡未知的状态。此程序需要在真机上进行调试才会显示正确的运行结果。这里主要是为了说明程序如何编写。

[html]
  1. 1. <?xml version="1.0" encoding="utf-8"?>   
  2. 2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3. 3.     android:orientation="vertical"   
  4. 4.     android:layout_width="fill_parent"   
  5. 5.     android:layout_height="fill_parent"   
  6. 6.     >   
  7. 7. <TextView     
  8. 8.     android:layout_width="fill_parent"    
  9. 9.     android:layout_height="wrap_content"    
  10. 10.     android:text="@string/hello"   
  11. 11.     />   
  12. 12. <Button   
  13. 13.     android:id="@+id/startButton"   
  14. 14.     android:layout_width="300dp"   
  15. 15.     android:layout_height="wrap_content"   
  16. 16.     android:text="打开WIFI网卡"   
  17. 17.     />   
  18. 18. <Button   
  19. 19.     android:id="@+id/stopButton"   
  20. 20.     android:layout_width="300dp"   
  21. 21.     android:layout_height="wrap_content"   
  22. 22.     android:text="关闭WIFI网卡"   
  23. 23.     />   
  24. 24. <Button   
  25. 25.     android:id="@+id/checkButton"   
  26. 26.     android:layout_width="300dp"   
  27. 27.     android:layout_height="wrap_content"   
  28. 28.     android:text="检查WIFI网卡状态"   
  29. 29.     />   
  30. 30. </LinearLayout>   
1. 
2.
7.
12.
18.
24.
30.
[java]
  1. 1. package idea.org;   
  2. 2.    
  3. 3. import android.app.Activity;   
  4. 4. import android.content.Context;   
  5. 5. import android.net.wifi.WifiManager;   
  6. 6. import android.os.Bundle;   
  7. 7. import android.view.View;   
  8. 8. import android.view.View.OnClickListener;   
  9. 9. import android.widget.Button;   
  10. 10. import android.widget.Toast;   
  11. 11.    
  12. 12. public class Android_Wifi extends Activity {   
  13. 13.     private Button startButton=null;   
  14. 14.     private Button stopButton=null;   
  15. 15.     private Button checkButton=null;   
  16. 16.     WifiManager wifiManager=null;   
  17. 17.     /** Called when the activity is first created. */   
  18. 18.     @Override   
  19. 19.     public void onCreate(Bundle savedInstanceState) {   
  20. 20.         super.onCreate(savedInstanceState);   
  21. 21.         setContentView(R.layout.main);   
  22. 22.         startButton=(Button)findViewById(R.id.startButton);   
  23. 23.         stopButton=(Button)findViewById(R.id.stopButton);   
  24. 24.         checkButton=(Button)findViewById(R.id.checkButton);   
  25. 25.         startButton.setOnClickListener(new startButtonListener());   
  26. 26.         stopButton.setOnClickListener(new stopButtonListener());   
  27. 27.         checkButton.setOnClickListener(new checkButtonListener());   
  28. 28.     }   
  29. 29.     class startButtonListener implements OnClickListener   
  30. 30.     {   
  31. 31.    
  32. 32.         /* (non-Javadoc)
  33. 33.          * @see android.view.View.OnClickListener#onClick(android.view.View)
  34. 34.          */   
  35. 35.         @Override   
  36. 36.         public void onClick(View v) {   
  37. 37.             // TODO Auto-generated method stub   
  38. 38.             wifiManager=(WifiManager)Android_Wifi.this.getSystemService(Context.WIFI_SERVICE);   
  39. 39.             wifiManager.setWifiEnabled(true);   
  40. 40.             System.out.println("wifi state --->"+wifiManager.getWifiState());   
  41. 41.             Toast.makeText(Android_Wifi.this, "当前网卡状态为:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();   
  42. 42.         }   
  43. 43.            
  44. 44.     }   
  45. 45.     class stopButtonListener implements OnClickListener   
  46. 46.     {   
  47. 47.    
  48. 48.         /* (non-Javadoc)
  49. 49.          * @see android.view.View.OnClickListener#onClick(android.view.View)
  50. 50.          */   
  51. 51.         @Override   
  52. 52.         public void onClick(View v) {   
  53. 53.             // TODO Auto-generated method stub   
  54. 54.             wifiManager=(WifiManager)Android_Wifi.this.getSystemService(Context.WIFI_SERVICE);   
  55. 55.             wifiManager.setWifiEnabled(false);   
  56. 56.             System.out.println("wifi state --->"+wifiManager.getWifiState());   
  57. 57.             Toast.makeText(Android_Wifi.this, "当前网卡状态为:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();   
  58. 58.         }   
  59. 59.            
  60. 60.     }   
  61. 61.     class checkButtonListener implements OnClickListener   
  62. 62.     {   
  63. 63.    
  64. 64.         /* (non-Javadoc)
  65. 65.          * @see android.view.View.OnClickListener#onClick(android.view.View)
  66. 66.          */   
  67. 67.         @Override   
  68. 68.         public void onClick(View v) {   
  69. 69.             // TODO Auto-generated method stub   
  70. 70.             wifiManager=(WifiManager)Android_Wifi.this.getSystemService(Context.WIFI_SERVICE);             
  71. 71.             System.out.println("wifi state --->"+wifiManager.getWifiState());   
  72. 72.             Toast.makeText(Android_Wifi.this, "当前网卡状态为:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();   
  73. 73.         }          
  74. 74.     }   
  75. 75. }   
1. package idea.org;     2.      3. import android.app.Activity;     4. import android.content.Context;     5. import android.net.wifi.WifiManager;     6. import android.os.Bundle;     7. import android.view.View;     8. import android.view.View.OnClickListener;     9. import android.widget.Button;    10. import android.widget.Toast;    11.     12. public class Android_Wifi extends Activity {    13.     private Button startButton=null;    14.     private Button stopButton=null;    15.     private Button checkButton=null;    16.     WifiManager wifiManager=null;    17.     /** Called when the activity is first created. */    18.     @Override    19.     public void onCreate(Bundle savedInstanceState) {    20.         super.onCreate(savedInstanceState);    21.         setContentView(R.layout.main);    22.         startButton=(Button)findViewById(R.id.startButton);    23.         stopButton=(Button)findViewById(R.id.stopButton);    24.         checkButton=(Button)findViewById(R.id.checkButton);    25.         startButton.setOnClickListener(new startButtonListener());    26.         stopButton.setOnClickListener(new stopButtonListener());    27.         checkButton.setOnClickListener(new checkButtonListener());    28.     }    29.     class startButtonListener implements OnClickListener    30.     {    31.     32.         /* (non-Javadoc)   33.          * @see android.view.View.OnClickListener#onClick(android.view.View)   34.          */    35.         @Override    36.         public void onClick(View v) {    37.             // TODO Auto-generated method stub    38.             wifiManager=(WifiManager)Android_Wifi.this.getSystemService(Context.WIFI_SERVICE);    39.             wifiManager.setWifiEnabled(true);    40.             System.out.println("wifi state --->"+wifiManager.getWifiState());    41.             Toast.makeText(Android_Wifi.this, "当前网卡状态为:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();    42.         }    43.             44.     }    45.     class stopButtonListener implements OnClickListener    46.     {    47.     48.         /* (non-Javadoc)   49.          * @see android.view.View.OnClickListener#onClick(android.view.View)   50.          */    51.         @Override    52.         public void onClick(View v) {    53.             // TODO Auto-generated method stub    54.             wifiManager=(WifiManager)Android_Wifi.this.getSystemService(Context.WIFI_SERVICE);    55.             wifiManager.setWifiEnabled(false);    56.             System.out.println("wifi state --->"+wifiManager.getWifiState());    57.             Toast.makeText(Android_Wifi.this, "当前网卡状态为:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();    58.         }    59.             60.     }    61.     class checkButtonListener implements OnClickListener    62.     {    63.     64.         /* (non-Javadoc)   65.          * @see android.view.View.OnClickListener#onClick(android.view.View)   66.          */    67.         @Override    68.         public void onClick(View v) {    69.             // TODO Auto-generated method stub    70.             wifiManager=(WifiManager)Android_Wifi.this.getSystemService(Context.WIFI_SERVICE);              71.             System.out.println("wifi state --->"+wifiManager.getWifiState());    72.             Toast.makeText(Android_Wifi.this, "当前网卡状态为:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();    73.         }           74.     }    75. }
[html]
  1. 1. <?xml version="1.0" encoding="utf-8"?>   
  2. 2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
  3. 3.       package="idea.org"   
  4. 4.       android:versionCode="1"   
  5. 5.       android:versionName="1.0">   
  6. 6.     <uses-sdk android:minSdkVersion="11" />   
  7. 7.    
  8. 8.     <application android:icon="@drawable/icon" android:label="@string/app_name">   
  9. 9.         <activity android:name=".Android_Wifi"   
  10. 10.                   android:label="@string/app_name">   
  11. 11.             <intent-filter>   
  12. 12.                 <action android:name="android.intent.action.MAIN" />   
  13. 13.                 <category android:name="android.intent.category.LAUNCHER" />   
  14. 14.             </intent-filter>   
  15. 15.         </activity>   
  16. 16.    
  17. 17.     </application>   
  18. 18.     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>   
  19. 19.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>   
  20. 20.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>   
  21. 21.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>   
  22. 22. </manifest>   
1. 
2.
6.
7. 8.
9.
11.
12.
13.
14.
15.
16. 17.
18.
19.
20.
21.
22.

依次点击“打开WIFI网卡”,“关闭WIFI网卡”,“检查WIFI网卡状态”三个Button按钮,控制台输出一下内容。

转自:

转载于:https://www.cnblogs.com/bmate/archive/2012/08/10/2631317.html

你可能感兴趣的文章
iOS学习之移除Main.storyboard
查看>>
感人至深的老情书
查看>>
String StringBuilder StringBuffer
查看>>
安装 ruby, sass 和 compass
查看>>
linux系统故障排除
查看>>
Python黑魔法:元类
查看>>
jQuery.extend()和 jQuery.fn.extend()用法总结
查看>>
Centos 7 学习安装步骤
查看>>
Linux常用命令简介
查看>>
2. 性能测试中常见术语集合
查看>>
内存rank概念和区分
查看>>
c++解惑之读取文件getline
查看>>
Spell Checker - 新版Chrome的纠错特性
查看>>
http协议以及httpd2.2与httpd2.4的详解
查看>>
jpa postgresql 使用uuid作为主键
查看>>
Linux文件目录
查看>>
8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重
查看>>
GAEPhotos V1.02 发布了
查看>>
docker 部署Tomcat应用相关操作
查看>>
文件权限
查看>>