ANDROID_MARS学习笔记_S01原始版_014_WIFI

一、代码
1.xml
(1)main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/startWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动手机的wifi网卡"
/>
<Button
android:id="@+id/stopWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭手机的wifi网卡"
/>
<Button
android:id="@+id/checkWifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="检查当前wifi网卡状态"
/>
</LinearLayout>

(2)AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.s01_original_e23_wifi"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".WifiActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 以下是使用wifi访问网络所需要的权限 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
</manifest>

2.java
(1)WifiActivity.java

 package com.example.s01_original_e23_wifi;

 import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class WifiActivity extends Activity {
/** Called when the activity is first created. */
private Button startButton = null;
private Button stopButton = null;
private Button checkButton = null;
private WifiManager wifiManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button)findViewById(R.id.startWifi);
stopButton = (Button)findViewById(R.id.stopWifi);
checkButton = (Button)findViewById(R.id.checkWifi);
startButton.setOnClickListener(new StartWifiListener());
stopButton.setOnClickListener(new StopWifiListener());
checkButton.setOnClickListener(new CheckWifiListener());
}
class StartWifiListener implements OnClickListener{ @Override
public void onClick(View v) {
wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
System.out.println("wifi state --->" + wifiManager.getWifiState());
Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
class StopWifiListener implements OnClickListener{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
System.out.println("wifi state --->" + wifiManager.getWifiState());
Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
} } class CheckWifiListener implements OnClickListener{ @Override
public void onClick(View v) {
wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
System.out.println("wifi state --->" + wifiManager.getWifiState());
Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
} }
}
上一篇:js中的this和箭头函数中的this


下一篇:【转载】 IP实时传输协议RTP/RTCP详解