WiFi的使用Demo

先发代码放上来:

package cn.com;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class WiFiTest extends Activity
{
private TextView mTextView01;
private CheckBox mCheckBox01;

/* 创建WiFiManager对象 */
private WifiManager mWiFiManager01;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTextView01 = (TextView) findViewById(R.id.myTextView1);
mCheckBox01 = (CheckBox) findViewById(R.id.myCheckBox1);

/* 以getSystemService取得WIFI_SERVICE */
mWiFiManager01 = (WifiManager)
this.getSystemService(Context.WIFI_SERVICE);

checkWiFiStatus();
addCheckboxListener();

}

public void addCheckboxListener()
{
/* 捕捉CheckBox的点击事件 */
mCheckBox01.setOnClickListener(new CheckBox.OnClickListener()
{
public void onClick(View v)
{
/* 当选取项为取消选取状态 */
if (mCheckBox01.isChecked() == false)
{
closeWiFiServices();
} else if (mCheckBox01.isChecked() == true)
{
openWiFiServices();
}
}
});
}

public void checkWiFiStatus()
{
if (mWiFiManager01.isWifiEnabled())
// 判断手机是否已启用了WiFi功能
{
/* 判断WiFi状态是否“已打开” */
if (mWiFiManager01.getWifiState() ==
WifiManager.WIFI_STATE_ENABLED)
{
/* 若WiFi已打开,将选取项打勾 */
mCheckBox01.setChecked(true);
/* 更改选取项文字为关闭WiFi */
mCheckBox01.setText(R.string.str_uncheck);
} else
{
/* 若WiFi未打开,将选取项勾选择消 */
mCheckBox01.setChecked(false);
/* 更改选取项文字为打开WiFi */
mCheckBox01.setText(R.string.str_checked);
}
} else
{
mCheckBox01.setChecked(false);
mCheckBox01.setText(R.string.str_checked);
}
}

public void mMakeTextToast(String str, boolean isLong)
{
if (isLong == true)
{
Toast.makeText(EX05_17.this, str, Toast.LENGTH_LONG).show();
} else
{
Toast.makeText(EX05_17.this, str, Toast.LENGTH_SHORT).show();
}
}

public void closeWiFiServices()
{
try
{
if (mWiFiManager01.isWifiEnabled())
// 判断WiFi状态是否为已打开
{
if (mWiFiManager01.setWifiEnabled(false))// 关闭WiFi
{
mTextView01.setText(R.string.str_stop_wifi_done);
} else
{
mTextView01.setText(R.string.str_stop_wifi_failed);
}
} else
{
switch (mWiFiManager01.getWifiState())
// WiFi状态不为已打开状态时
{
case WifiManager.WIFI_STATE_ENABLING:
// WiFi正在打开过程中,导致无法关闭...
mTextView01.setText(getResources().getText(
R.string.str_stop_wifi_failed)
+ ":" +
getResources().getText(R.string.str_wifi_enabling));
break;
case WifiManager.WIFI_STATE_DISABLING:
// WiFi正在关闭过程中,导致无法关闭...
mTextView01.setText(getResources().getText(
R.string.str_stop_wifi_failed)
+ ":" +
getResources().getText(R.string.str_wifi_disabling));
break;
case WifiManager.WIFI_STATE_DISABLED:
// WiFi已经关闭
mTextView01.setText(getResources().getText(
R.string.str_stop_wifi_failed)
+ ":" +
getResources().getText(R.string.str_wifi_disabled));
break;
case WifiManager.WIFI_STATE_UNKNOWN:
// 无法取得或辨识WiFi状态
default:
mTextView01.setText(getResources().getText(
R.string.str_stop_wifi_failed)
+ ":" +
getResources().getText(R.string.str_wifi_unknow));
break;
}
mCheckBox01.setText(R.string.str_checked);
}
} catch (Exception e)
{
Log.i("HIPPO", e.toString());
e.printStackTrace();
}
}

public void openWiFiServices()
{
if (!mWiFiManager01.isWifiEnabled()
// 确认WiFi服务是关闭且不在打开过程中
&& mWiFiManager01.getWifiState() !=
WifiManager.WIFI_STATE_ENABLING)
{
if (mWiFiManager01.setWifiEnabled(true))
// 启动WiFi服务
{
switch (mWiFiManager01.getWifiState())
{
case WifiManager.WIFI_STATE_ENABLING:
// WiFi正在打开过程中....
mTextView01.setText(getResources().getText(
R.string.str_wifi_enabling));
break;
case WifiManager.WIFI_STATE_ENABLED:
// WiFi已经打开
mTextView01.setText(getResources().getText(
R.string.str_start_wifi_done));
break;
default:
mTextView01.setText(getResources().getText(
R.string.str_start_wifi_failed)
// 其它未知的错误
+ ":" +
getResources().getText(R.string.str_wifi_unknow));
break;
}
} else
{
mTextView01.setText(R.string.str_start_wifi_failed);
}
}
}

}


还有个最重要的环节就是设置各种权限








:

你可能感兴趣的:(Android,OS,.net,Android(手机服务))