Android WiFi管理(WIFI_SERVICE)

Android WiFi管理(WIFI_SERVICE)

分类: Android2011-11-24 10:52 2000人阅读 评论(1) 收藏 举报

Android WiFi管理(WIFI_SERVICE)

  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. <uses-sdk android:minSdkVersion="7" />
  7. <application
  8. android:icon="@drawable/ic_launcher"
  9. android:label="@string/app_name" android:debuggable="true">
  10. <activity
  11. android:label="@string/app_name"
  12. android:name=".WifiManagerActivity" >
  13. <intent-filter >
  14. <action android:name="android.intent.action.MAIN" />
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. </application>
  19. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  20. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  21. <uses-permission android:name="android.permission.WAKE_LOCK" />
  22. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  23. </manifest>
  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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <Button
  11. android:id="@+id/wifiManager_open"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="打开WiFi" />
  15. <Button
  16. android:id="@+id/wifiManager_close"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="关闭WiFi" />
  20. <Button
  21. android:id="@+id/wifiManager_check"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="显示WiFi状态" />
  25. <Button
  26. android:id="@+id/wifiManager_WIFI_SETTINGS"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:text="WiFi网络设置" />
  30. <Button
  31. android:id="@+id/threeGManager_State"
  32. android:layout_width="fill_parent"
  33. android:layout_height="wrap_content"
  34. android:text="显示3G边境状态" />
  35. <Button
  36. android:id="@+id/wifiManager_WIRELESS_SETTINGS"
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:text="无线网络配置" />
  40. </LinearLayout>
  1. package com.entel.research;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.ConnectivityManager;
  6. import android.net.NetworkInfo.State;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Bundle;
  9. import android.provider.Settings;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14. public class WifiManagerActivity extends Activity
  15. {
  16. @Override
  17. public void onCreate(Bundle savedInstanceState)
  18. {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. Button wifiManager_open = (Button) findViewById(R.id.wifiManager_open);
  22. Button wifiManager_close = (Button) findViewById(R.id.wifiManager_close);
  23. Button wifiManager_check = (Button) findViewById(R.id.wifiManager_check);
  24. Button wifiManager_WIFI_SETTINGS = (Button) findViewById(R.id.wifiManager_WIFI_SETTINGS);
  25. Button wifiManager_WIRELESS_SETTINGS = (Button) findViewById(R.id.wifiManager_WIRELESS_SETTINGS);
  26. Button threeGManager_State = (Button) findViewById(R.id.threeGManager_State);
  27. final WifiManager wifiManager = (WifiManager) WifiManagerActivity.this
  28. .getSystemService(Context.WIFI_SERVICE);
  29. final ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  30. wifiManager_open.setOnClickListener(new OnClickListener()
  31. {
  32. public void onClick(View v)
  33. {
  34. wifiManager.setWifiEnabled(true);
  35. Toast.makeText(WifiManagerActivity.this, "Wifi开启",
  36. Toast.LENGTH_SHORT).show();
  37. }
  38. });
  39. wifiManager_close.setOnClickListener(new OnClickListener()
  40. {
  41. public void onClick(View v)
  42. {
  43. if (wifiManager.isWifiEnabled())
  44. {
  45. wifiManager.setWifiEnabled(false);
  46. Toast.makeText(WifiManagerActivity.this, "Wifi关闭",
  47. Toast.LENGTH_SHORT).show();
  48. }
  49. Toast.makeText(WifiManagerActivity.this, "Wifi关闭",
  50. Toast.LENGTH_SHORT).show();
  51. }
  52. });
  53. wifiManager_check.setOnClickListener(new OnClickListener()
  54. {
  55. public void onClick(View v)
  56. {
  57. String result = null;
  58. switch (wifiManager.getWifiState())
  59. {
  60. case WifiManager.WIFI_STATE_DISABLED:
  61. result = "WIFI已关闭";
  62. break;
  63. case WifiManager.WIFI_STATE_DISABLING:
  64. result = "WIFI正在关闭中";
  65. break;
  66. case WifiManager.WIFI_STATE_ENABLED:
  67. result = "WIFI已启用";
  68. break;
  69. case WifiManager.WIFI_STATE_ENABLING:
  70. result = "WIFI正在启动中";
  71. break;
  72. case WifiManager.WIFI_STATE_UNKNOWN:
  73. result = "未知WIFI状态";
  74. break;
  75. }
  76. Toast.makeText(WifiManagerActivity.this, result, Toast.LENGTH_SHORT)
  77. .show();
  78. }
  79. });
  80. wifiManager_WIFI_SETTINGS.setOnClickListener(new OnClickListener()
  81. {
  82. public void onClick(View v)
  83. {
  84. startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
  85. }
  86. });
  87. threeGManager_State.setOnClickListener(new OnClickListener()
  88. {
  89. public void onClick(View v)
  90. {
  91. State mobile = conMan.getNetworkInfo(
  92. ConnectivityManager.TYPE_MOBILE).getState();
  93. Toast.makeText(WifiManagerActivity.this, mobile.toString(),
  94. Toast.LENGTH_SHORT).show();
  95. }
  96. });
  97. wifiManager_WIRELESS_SETTINGS.setOnClickListener(new OnClickListener()
  98. {
  99. public void onClick(View v)
  100. {
  101. startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
  102. }
  103. });
  104. }
  105. }

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

http://blog.csdn.net/sbvfhp/article/details/7007090
http://www.2cto.com/kf/201111/112219.html
上一篇:Qt程序启动画面播放(gif与swf两种动画格式)


下一篇:qt程序启动画面