package cn.android.app;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class WifiActivity extends Activity {
/** Called when the activity is first created. */
/***
*
* @author Administrator 今天老师讲了一个简单的wifi查看 好吧 自己來敲一敲 加油 記住
* 這裡我們要添加兩個權限在配置文件裡賣
* */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// 在寫這個項目之前呢,我覺得還是有必要去看一下文檔.
// 先建立一個mananger
WifiManager wifiMananger = null;
public void onAction(View v) {
// 這裡我們去得到mananager
wifiMananger = (WifiManager) this
.getSystemService(Context.WIFI_SERVICE);
int state = wifiMananger.getWifiState();
switch (v.getId()) {
case R.id.open:
if (state != 3) {
Toast.makeText(
this,
"this wifi's state is " + state
+ "and now to open the wifi!",
Toast.LENGTH_SHORT).show();
}
wifiMananger.setWifiEnabled(true);
break;
case R.id.close:
if (state != 2) {
if (state != 3) {
Toast.makeText(
this,
"this wifi's state is " + state
+ "and now to close the wifi!",
Toast.LENGTH_SHORT).show();
}
wifiMananger.setWifiEnabled(false);
}
break;
case R.id.check:
Toast.makeText(this, "this wifi's state is " + state,
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
ok啦!