Android WiFi管理(WIFI_SERVICE)

  • 时间:
  • 出处:跟我学网络
  • 作者:
  • 浏览:657

标签:

 

Android WiFi管理(WIFI_SERVICE)

分类: Android
androidservicebuttonlayoutencodingmobile

 

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.entel.research"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="7" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" android:debuggable="true">  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:name=".WifiManagerActivity" >  
  15.             <intent-filter >  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  24.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  25.     <uses-permission android:name="android.permission.WAKE_LOCK" />  
  26.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  27.   
  28. </manifest>  

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/wifiManager_open"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="打开WiFi" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/wifiManager_close"  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="关闭WiFi" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/wifiManager_check"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="显示WiFi状态" />  
  29.   
  30.     <Button  
  31.         android:id="@+id/wifiManager_WIFI_SETTINGS"  
  32.         android:layout_width="fill_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="WiFi网络设置" />  
  35.   
  36.     <Button  
  37.         android:id="@+id/threeGManager_State"  
  38.         android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:text="显示3G边境状态" />  
  41.       
  42.     <Button  
  43.         android:id="@+id/wifiManager_WIRELESS_SETTINGS"  
  44.         android:layout_width="fill_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:text="无线网络配置" />  
  47.   
  48. </LinearLayout>  


[java] view plaincopy
  1. package com.entel.research;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.net.ConnectivityManager;  
  7. import android.net.NetworkInfo.State;  
  8. import android.net.wifi.WifiManager;  
  9. import android.os.Bundle;  
  10. import android.provider.Settings;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.   
  16. public class WifiManagerActivity extends Activity  
  17. {  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState)  
  20.     {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         Button wifiManager_open = (Button) findViewById(R.id.wifiManager_open);  
  25.         Button wifiManager_close = (Button) findViewById(R.id.wifiManager_close);  
  26.         Button wifiManager_check = (Button) findViewById(R.id.wifiManager_check);  
  27.         Button wifiManager_WIFI_SETTINGS = (Button) findViewById(R.id.wifiManager_WIFI_SETTINGS);  
  28.         Button wifiManager_WIRELESS_SETTINGS = (Button) findViewById(R.id.wifiManager_WIRELESS_SETTINGS);  
  29.           
  30.         Button threeGManager_State = (Button) findViewById(R.id.threeGManager_State);  
  31.   
  32.         final WifiManager wifiManager = (WifiManager) WifiManagerActivity.this  
  33.                 .getSystemService(Context.WIFI_SERVICE);  
  34.         final ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  35.   
  36.         wifiManager_open.setOnClickListener(new OnClickListener()  
  37.         {  
  38.             public void onClick(View v)  
  39.             {  
  40.                 wifiManager.setWifiEnabled(true);  
  41.                 Toast.makeText(WifiManagerActivity.this, "Wifi开启",  
  42.                         Toast.LENGTH_SHORT).show();  
  43.             }  
  44.         });  
  45.           
  46.         wifiManager_close.setOnClickListener(new OnClickListener()  
  47.         {  
  48.             public void onClick(View v)  
  49.             {  
  50.                 if (wifiManager.isWifiEnabled())  
  51.                 {  
  52.                     wifiManager.setWifiEnabled(false);  
  53.                     Toast.makeText(WifiManagerActivity.this, "Wifi关闭",  
  54.                             Toast.LENGTH_SHORT).show();  
  55.                 }  
  56.                 Toast.makeText(WifiManagerActivity.this, "Wifi关闭",  
  57.                         Toast.LENGTH_SHORT).show();  
  58.             }  
  59.         });  
  60.           
  61.         wifiManager_check.setOnClickListener(new OnClickListener()  
  62.         {  
  63.             public void onClick(View v)  
  64.             {  
  65.                 String result = null;  
  66.                 switch (wifiManager.getWifiState())  
  67.                 {  
  68.                     case WifiManager.WIFI_STATE_DISABLED:  
  69.                         result = "WIFI已关闭";  
  70.                         break;  
  71.                     case WifiManager.WIFI_STATE_DISABLING:  
  72.                         result = "WIFI正在关闭中";  
  73.                         break;  
  74.                     case WifiManager.WIFI_STATE_ENABLED:  
  75.                         result = "WIFI已启用";  
  76.                         break;  
  77.                     case WifiManager.WIFI_STATE_ENABLING:  
  78.                         result = "WIFI正在启动中";  
  79.                         break;  
  80.                     case WifiManager.WIFI_STATE_UNKNOWN:  
  81.                         result = "未知WIFI状态";  
  82.                         break;  
  83.                 }  
  84.                 Toast.makeText(WifiManagerActivity.this, result, Toast.LENGTH_SHORT)  
  85.                         .show();  
  86.             }  
  87.         });  
  88.           
  89.         wifiManager_WIFI_SETTINGS.setOnClickListener(new OnClickListener()  
  90.         {  
  91.             public void onClick(View v)  
  92.             {  
  93.                 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));  
  94.             }  
  95.         });  
  96.           
  97.         threeGManager_State.setOnClickListener(new OnClickListener()  
  98.         {  
  99.             public void onClick(View v)  
  100.             {  
  101.                 State mobile = conMan.getNetworkInfo(  
  102.                         ConnectivityManager.TYPE_MOBILE).getState();  
  103.                 Toast.makeText(WifiManagerActivity.this, mobile.toString(),  
  104.                         Toast.LENGTH_SHORT).show();  
  105.             }  
  106.         });  
  107.           
  108.         wifiManager_WIRELESS_SETTINGS.setOnClickListener(new OnClickListener()  
  109.         {  
  110.             public void onClick(View v)  
  111.             {  
  112.                 startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));  
  113.             }  
  114.         });  
  115.     }  
  116. }  


版权声明:本文为博主原创文章,未经博主允许不得转载。


http://blog.csdn.net/sbvfhp/article/details/7007090
http://www.2cto.com/kf/201111/112219.html