EX05_17.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:10k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex05_17;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.net.wifi.WifiManager;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.CheckBox;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. public class EX05_17 extends Activity
  12. {
  13.   private TextView mTextView01;
  14.   private CheckBox mCheckBox01;
  15.   
  16.   /* 创建WiFiManager对象 */
  17.   private WifiManager mWiFiManager01;
  18.   
  19.   /** Called when the activity is first created. */
  20.   @Override
  21.   public void onCreate(Bundle savedInstanceState)
  22.   {
  23.     super.onCreate(savedInstanceState);
  24.     setContentView(R.layout.main);
  25.     
  26.     mTextView01 = (TextView) findViewById(R.id.myTextView1);
  27.     mCheckBox01 = (CheckBox) findViewById(R.id.myCheckBox1);
  28.     
  29.     /* 以getSystemService取得WIFI_SERVICE */
  30.     mWiFiManager01 = (WifiManager)
  31.                      this.getSystemService(Context.WIFI_SERVICE);
  32.     
  33.     /* 判断运行程序后的WiFi状态是否打开或打开中 */
  34.     if(mWiFiManager01.isWifiEnabled())
  35.     {
  36.       /* 判断WiFi状态是否“已打开” */
  37.       if(mWiFiManager01.getWifiState()==
  38.          WifiManager.WIFI_STATE_ENABLED)
  39.       {
  40.         /* 若WiFi已打开,将选取项打勾 */
  41.         mCheckBox01.setChecked(true);
  42.         /* 更改选取项文字为关闭WiFi*/
  43.         mCheckBox01.setText(R.string.str_uncheck);
  44.       }
  45.       else
  46.       {
  47.         /* 若WiFi未打开,将选取项勾选择消 */
  48.         mCheckBox01.setChecked(false);
  49.         /* 更改选取项文字为打开WiFi*/
  50.         mCheckBox01.setText(R.string.str_checked);
  51.       }
  52.     }
  53.     else
  54.     {
  55.       mCheckBox01.setChecked(false);
  56.       mCheckBox01.setText(R.string.str_checked);
  57.     }
  58.     
  59.     /* 捕捉CheckBox的点击事件 */
  60.     mCheckBox01.setOnClickListener(
  61.     new CheckBox.OnClickListener()
  62.     {
  63.       @Override
  64.       public void onClick(View v)
  65.       {
  66.         // TODO Auto-generated method stub
  67.         
  68.         /* 当选取项为取消选取状态 */
  69.         if(mCheckBox01.isChecked()==false)
  70.         {
  71.           /* 尝试关闭Wi-Fi服务 */
  72.           try
  73.           {
  74.             /* 判断WiFi状态是否为已打开 */
  75.             if(mWiFiManager01.isWifiEnabled() )
  76.             {
  77.               /* 关闭WiFi */
  78.               if(mWiFiManager01.setWifiEnabled(false))
  79.               {
  80.                 mTextView01.setText(R.string.str_stop_wifi_done);
  81.               }
  82.               else
  83.               {
  84.                 mTextView01.setText(R.string.str_stop_wifi_failed);
  85.               }
  86.             }
  87.             else
  88.             {
  89.               /* WiFi状态不为已打开状态时 */
  90.               switch(mWiFiManager01.getWifiState())
  91.               {
  92.                 /* WiFi正在打开过程中,导致无法关闭... */
  93.                 case WifiManager.WIFI_STATE_ENABLING:
  94.                   mTextView01.setText
  95.                   (
  96.                     getResources().getText
  97.                     (R.string.str_stop_wifi_failed)+":"+
  98.                     getResources().getText
  99.                     (R.string.str_wifi_enabling)
  100.                   );
  101.                   break;
  102.                 /* WiFi正在关闭过程中,导致无法关闭... */
  103.                 case WifiManager.WIFI_STATE_DISABLING:
  104.                   mTextView01.setText
  105.                   (
  106.                     getResources().getText
  107.                     (R.string.str_stop_wifi_failed)+":"+
  108.                     getResources().getText
  109.                     (R.string.str_wifi_disabling)
  110.                   );
  111.                   break;
  112.                 /* WiFi已经关闭 */
  113.                 case WifiManager.WIFI_STATE_DISABLED:
  114.                   mTextView01.setText
  115.                   (
  116.                     getResources().getText
  117.                     (R.string.str_stop_wifi_failed)+":"+
  118.                     getResources().getText
  119.                     (R.string.str_wifi_disabled)
  120.                   );
  121.                   break;
  122.                 /* 无法取得或辨识WiFi状态 */
  123.                 case WifiManager.WIFI_STATE_UNKNOWN:
  124.                 default:
  125.                   mTextView01.setText
  126.                   (
  127.                     getResources().getText
  128.                     (R.string.str_stop_wifi_failed)+":"+
  129.                     getResources().getText
  130.                     (R.string.str_wifi_unknow)
  131.                   );
  132.                   break;
  133.               }
  134.               mCheckBox01.setText(R.string.str_checked);
  135.             }
  136.           }
  137.           catch (Exception e)
  138.           {
  139.             Log.i("HIPPO", e.toString());
  140.             e.printStackTrace();
  141.           }
  142.         }
  143.         else if(mCheckBox01.isChecked()==true)
  144.         {
  145.           /* 尝试打开Wi-Fi服务 */
  146.           try
  147.           {
  148.             /* 确认WiFi服务是关闭且不在打开作业中 */
  149.             if(!mWiFiManager01.isWifiEnabled() && 
  150.                mWiFiManager01.getWifiState()!=
  151.                WifiManager.WIFI_STATE_ENABLING )
  152.             {
  153.               if(mWiFiManager01.setWifiEnabled(true))
  154.               {
  155.                 switch(mWiFiManager01.getWifiState())
  156.                 {
  157.                   /* WiFi正在打开过程中,导致无法打开... */
  158.                   case WifiManager.WIFI_STATE_ENABLING:
  159.                     mTextView01.setText
  160.                     (
  161.                       getResources().getText
  162.                       (R.string.str_wifi_enabling)
  163.                     );
  164.                     break;
  165.                   /* WiFi已经为打开,无法再次打开... */
  166.                   case WifiManager.WIFI_STATE_ENABLED:
  167.                     mTextView01.setText
  168.                     (
  169.                       getResources().getText
  170.                       (R.string.str_start_wifi_done)
  171.                     );
  172.                     break;
  173.                   /* 其它未知的错误 */
  174.                   default:
  175.                     mTextView01.setText
  176.                     (
  177.                       getResources().getText
  178.                       (R.string.str_start_wifi_failed)+":"+
  179.                       getResources().getText
  180.                       (R.string.str_wifi_unknow)
  181.                     );
  182.                     break;
  183.                 }
  184.               }
  185.               else
  186.               {
  187.                 mTextView01.setText(R.string.str_start_wifi_failed);
  188.               }
  189.             }
  190.             else
  191.             {
  192.               switch(mWiFiManager01.getWifiState())
  193.               {
  194.                 /* WiFi正在打开过程中,导致无法打开... */
  195.                 case WifiManager.WIFI_STATE_ENABLING:
  196.                   mTextView01.setText
  197.                   (
  198.                     getResources().getText
  199.                     (R.string.str_start_wifi_failed)+":"+
  200.                     getResources().getText
  201.                     (R.string.str_wifi_enabling)
  202.                   );
  203.                   break;
  204.                 /* WiFi正在关闭过程中,导致无法打开... */
  205.                 case WifiManager.WIFI_STATE_DISABLING:
  206.                   mTextView01.setText
  207.                   (
  208.                     getResources().getText
  209.                     (R.string.str_start_wifi_failed)+":"+
  210.                     getResources().getText
  211.                     (R.string.str_wifi_disabling)
  212.                   );
  213.                   break;
  214.                 /* WiFi已经关闭 */
  215.                 case WifiManager.WIFI_STATE_DISABLED:
  216.                   mTextView01.setText
  217.                   (
  218.                     getResources().getText
  219.                     (R.string.str_start_wifi_failed)+":"+
  220.                     getResources().getText
  221.                     (R.string.str_wifi_disabled)
  222.                   );
  223.                   break;
  224.                 /* 无法取得或识别WiFi状态 */
  225.                 case WifiManager.WIFI_STATE_UNKNOWN:
  226.                 default:
  227.                   mTextView01.setText
  228.                   (
  229.                     getResources().getText
  230.                     (R.string.str_start_wifi_failed)+":"+
  231.                     getResources().getText
  232.                     (R.string.str_wifi_unknow)
  233.                   );
  234.                   break;
  235.               }
  236.             }
  237.             mCheckBox01.setText(R.string.str_uncheck);
  238.           }
  239.           catch (Exception e)
  240.           {
  241.             Log.i("HIPPO", e.toString());
  242.             e.printStackTrace();
  243.           }
  244.         }
  245.       }
  246.     });
  247.   }
  248.   
  249.   public void mMakeTextToast(String str, boolean isLong)
  250.   {
  251.     if(isLong==true)
  252.     {
  253.       Toast.makeText(EX05_17.this, str, Toast.LENGTH_LONG).show();
  254.     }
  255.     else
  256.     {
  257.       Toast.makeText(EX05_17.this, str, Toast.LENGTH_SHORT).show();
  258.     }
  259.   }
  260.   
  261.   @Override
  262.   protected void onResume()
  263.   {
  264.     // TODO Auto-generated method stub
  265.     
  266.     /* 在onResume重写事件为取得打开程序当下WiFi的状态 */
  267.     try
  268.     {
  269.       switch(mWiFiManager01.getWifiState())
  270.       {
  271.         /* WiFi已经在打开状态... */
  272.         case WifiManager.WIFI_STATE_ENABLED:
  273.           mTextView01.setText
  274.           (
  275.             getResources().getText(R.string.str_wifi_enabling)
  276.           );
  277.           break;
  278.         /* WiFi正在打开过程中状态... */
  279.         case WifiManager.WIFI_STATE_ENABLING:
  280.           mTextView01.setText
  281.           (
  282.             getResources().getText(R.string.str_wifi_enabling)
  283.           );
  284.           break;
  285.         /* WiFi正在关闭过程中... */
  286.         case WifiManager.WIFI_STATE_DISABLING:
  287.           mTextView01.setText
  288.           (
  289.             getResources().getText(R.string.str_wifi_disabling)
  290.           );
  291.           break;
  292.         /* WiFi已经关闭 */
  293.         case WifiManager.WIFI_STATE_DISABLED:
  294.           mTextView01.setText
  295.           (
  296.             getResources().getText(R.string.str_wifi_disabled)
  297.           );
  298.           break;
  299.         /* 无法取得或识别WiFi状态 */
  300.         case WifiManager.WIFI_STATE_UNKNOWN:
  301.         default:
  302.           mTextView01.setText
  303.           (
  304.             getResources().getText(R.string.str_wifi_unknow)
  305.           );
  306.           break;
  307.       }
  308.     }
  309.     catch(Exception e)
  310.     {
  311.       mTextView01.setText(e.toString());
  312.       e.getStackTrace();
  313.     }
  314.     super.onResume();
  315.   }
  316.   
  317.   @Override
  318.   protected void onPause()
  319.   {
  320.     // TODO Auto-generated method stub
  321.     super.onPause();
  322.   }
  323. }