ANDROID_MARS学习笔记_S01原始版_014_WIFI

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <Button
 8     android:id="@+id/startWifi"
 9     android:layout_width="fill_parent"
10     android:layout_height="wrap_content"
11     android:text="启动手机的wifi网卡"
12     />
13 <Button
14     android:id="@+id/stopWifi"
15     android:layout_width="fill_parent"
16     android:layout_height="wrap_content"
17     android:text="关闭手机的wifi网卡"
18     />
19 <Button
20     android:id="@+id/checkWifi"
21     android:layout_width="fill_parent"
22     android:layout_height="wrap_content"
23     android:text="检查当前wifi网卡状态"
24     />
25 </LinearLayout>

(2)AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.s01_original_e23_wifi"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="21" />
10
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name=".WifiActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26     <!-- 以下是使用wifi访问网络所需要的权限 -->
27     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
28     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
29     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
30     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
31 </manifest>

2.java
(1)WifiActivity.java

 1 package com.example.s01_original_e23_wifi;
 2
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.net.wifi.WifiManager;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.Toast;
11
12 public class WifiActivity extends Activity {
13     /** Called when the activity is first created. */
14     private Button startButton = null;
15     private Button stopButton = null;
16     private Button checkButton = null;
17     private WifiManager wifiManager = null;
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22         startButton = (Button)findViewById(R.id.startWifi);
23         stopButton = (Button)findViewById(R.id.stopWifi);
24         checkButton = (Button)findViewById(R.id.checkWifi);
25         startButton.setOnClickListener(new StartWifiListener());
26         stopButton.setOnClickListener(new StopWifiListener());
27         checkButton.setOnClickListener(new CheckWifiListener());
28     }
29     class StartWifiListener implements OnClickListener{
30
31         @Override
32         public void onClick(View v) {
33             wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
34             wifiManager.setWifiEnabled(true);
35             System.out.println("wifi state --->" + wifiManager.getWifiState());
36             Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
37         }
38     }
39     class StopWifiListener implements OnClickListener{
40
41         @Override
42         public void onClick(View arg0) {
43             // TODO Auto-generated method stub
44             wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
45             wifiManager.setWifiEnabled(false);
46             System.out.println("wifi state --->" + wifiManager.getWifiState());
47             Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
48         }
49
50     }
51
52     class CheckWifiListener implements OnClickListener{
53
54         @Override
55         public void onClick(View v) {
56             wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
57             System.out.println("wifi state --->" + wifiManager.getWifiState());
58             Toast.makeText(WifiActivity.this, "当前无线网卡状态:" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
59         }
60
61     }
62 }
时间: 2024-10-12 22:38:40

ANDROID_MARS学习笔记_S01原始版_014_WIFI的相关文章

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过android.media.MediaPlayer实现播放,暂停.停止 二.代码1.xml(1)player.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:andr

ANDROID_MARS学习笔记_S01原始版_010_ContentProvider

一.简介 一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill

ANDROID_MARS学习笔记_S01原始版_005_ProgressBar

一.代码 1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_p

ANDROID_MARS学习笔记_S01原始版_006_ListView

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_pare

ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

一.代码 1.xml(1)radio.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_