Android WiFi管理(WIFI_SERVICE)
2015-09-21 15:03
513 查看
Android WiFi管理(WIFI_SERVICE)
分类: Android2011-11-24 10:52 2000人阅读 评论(1) 收藏 举报androidservicebuttonlayoutencodingmobile
![](http://hi.csdn.net/attachment/201111/24/0_1322103128chpP.gif)
[html] view plaincopy
<?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>
[html] view plaincopy
<?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>
[java] view plaincopy
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/7007090http://www.2cto.com/kf/201111/112219.html
相关文章推荐
- android 观察者模式
- android是如何加载资源图片的?
- Android 摇一摇之双甩功能
- Android:单元测试
- android打印 编辑并打印 word
- Android:查看应用创建的数据库
- Android:简单联网获取网页代码
- Android:数据存储之SQLite
- Android:设计之屏幕适配
- Android图片压缩(质量压缩和尺寸压缩)、图片保存(本地和sqlite数据库)
- Android:控件的隐藏显示
- Android:利用SharedPreferences实现自动登录
- 【小知识点总结】Android在java代码中设置margin
- Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
- AndroidManifest简述
- RadioButton - android 自定义RadioButton样式
- Android:储存方式之SharePreferences
- Android:实现数组之间的复制
- Android:控件AutoCompleteTextView 自动提示
- 【小知识点总结】如何获取到Android控件的高度