Android WiFi管理(WIFI_SERVICE)
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.entel.research"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="7" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" android:debuggable="true">
- <activity
- android:label="@string/app_name"
- android:name=".WifiManagerActivity" >
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.WAKE_LOCK" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:id="@+id/wifiManager_open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="打开WiFi" />
- <Button
- android:id="@+id/wifiManager_close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="关闭WiFi" />
- <Button
- android:id="@+id/wifiManager_check"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="显示WiFi状态" />
- <Button
- android:id="@+id/wifiManager_WIFI_SETTINGS"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="WiFi网络设置" />
- <Button
- android:id="@+id/threeGManager_State"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="显示3G边境状态" />
- <Button
- android:id="@+id/wifiManager_WIRELESS_SETTINGS"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="无线网络配置" />
- </LinearLayout>
- package com.entel.research;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo.State;
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class WifiManagerActivity extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button wifiManager_open = (Button) findViewById(R.id.wifiManager_open);
- Button wifiManager_close = (Button) findViewById(R.id.wifiManager_close);
- Button wifiManager_check = (Button) findViewById(R.id.wifiManager_check);
- Button wifiManager_WIFI_SETTINGS = (Button) findViewById(R.id.wifiManager_WIFI_SETTINGS);
- Button wifiManager_WIRELESS_SETTINGS = (Button) findViewById(R.id.wifiManager_WIRELESS_SETTINGS);
- Button threeGManager_State = (Button) findViewById(R.id.threeGManager_State);
- final WifiManager wifiManager = (WifiManager) WifiManagerActivity.this
- .getSystemService(Context.WIFI_SERVICE);
- final ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- wifiManager_open.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- wifiManager.setWifiEnabled(true);
- Toast.makeText(WifiManagerActivity.this, "Wifi开启",
- Toast.LENGTH_SHORT).show();
- }
- });
- wifiManager_close.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- if (wifiManager.isWifiEnabled())
- {
- wifiManager.setWifiEnabled(false);
- Toast.makeText(WifiManagerActivity.this, "Wifi关闭",
- Toast.LENGTH_SHORT).show();
- }
- Toast.makeText(WifiManagerActivity.this, "Wifi关闭",
- Toast.LENGTH_SHORT).show();
- }
- });
- wifiManager_check.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- String result = null;
- switch (wifiManager.getWifiState())
- {
- case WifiManager.WIFI_STATE_DISABLED:
- result = "WIFI已关闭";
- break;
- case WifiManager.WIFI_STATE_DISABLING:
- result = "WIFI正在关闭中";
- break;
- case WifiManager.WIFI_STATE_ENABLED:
- result = "WIFI已启用";
- break;
- case WifiManager.WIFI_STATE_ENABLING:
- result = "WIFI正在启动中";
- break;
- case WifiManager.WIFI_STATE_UNKNOWN:
- result = "未知WIFI状态";
- break;
- }
- Toast.makeText(WifiManagerActivity.this, result, Toast.LENGTH_SHORT)
- .show();
- }
- });
- wifiManager_WIFI_SETTINGS.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
- }
- });
- threeGManager_State.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- State mobile = conMan.getNetworkInfo(
- ConnectivityManager.TYPE_MOBILE).getState();
- Toast.makeText(WifiManagerActivity.this, mobile.toString(),
- Toast.LENGTH_SHORT).show();
- }
- });
- wifiManager_WIRELESS_SETTINGS.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
- }
- });
- }
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 上一篇Android 面试题
- 下一篇Android连接到加密网络
- http://blog.csdn.net/sbvfhp/article/details/7007090
- http://www.2cto.com/kf/201111/112219.html