Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208407
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-11 09:33:55

01_23_WIFI网络操作




点击(此处)折叠或打开

  1. //WifiActivity.java
  2. package mars.wifi;

  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. public class WifiActivity extends Activity {
  12.     /** Called when the activity is first created. */
  13.     private Button startButton = null;
  14.     private Button stopButton = null;
  15.     private Button checkButton = null;
  16.     private WifiManager wifiManager = null;
  17.     @Override
  18.     public void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.main);
  21.         startButton = (Button)findViewById(R.id.startWifi);
  22.         stopButton = (Button)findViewById(R.id.stopWifi);
  23.         checkButton = (Button)findViewById(R.id.checkWifi);
  24.         startButton.setOnClickListener(new StartWifiListener());
  25.         stopButton.setOnClickListener(new StopWifiListener());
  26.         checkButton.setOnClickListener(new CheckWifiListener());
  27.     }
  28.     class StartWifiListener implements OnClickListener{

  29.         @Override
  30.         public void onClick(View v) {
  31.             wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
  32.             wifiManager.setWifiEnabled(true);
  33.             System.out.println("wifi state --->" + wifiManager.getWifiState());
  34.             Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
  35.         }
  36.     }
  37.     class StopWifiListener implements OnClickListener{

  38.         @Override
  39.         public void onClick(View arg0) {
  40.             // TODO Auto-generated method stub
  41.             wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
  42.             wifiManager.setWifiEnabled(false);
  43.             System.out.println("wifi state --->" + wifiManager.getWifiState());
  44.             Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
  45.         }
  46.     
  47.     }
  48.     
  49.     class CheckWifiListener implements OnClickListener{

  50.         @Override
  51.         public void onClick(View v) {
  52.             wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);
  53.             System.out.println("wifi state --->" + wifiManager.getWifiState());
  54.             Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
  55.         }
  56.         
  57.     }
  58. }

注意在AndroidMainfest.xml中添加  改变网络状态、改变WIFI状态 、访问网络状态、访问WIFI状态权限

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="mars.wifi" android:versionCode="1" android:versionName="1.0">
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  5.         <activity android:name=".WifiActivity" android:label="@string/app_name">
  6.             <intent-filter>
  7.                 <action android:name="android.intent.action.MAIN" />
  8.                 <category android:name="android.intent.category.LAUNCHER" />
  9.             </intent-filter>
  10.         </activity>

  11.     </application>
  12.     <uses-sdk android:minSdkVersion="4" />
  13.     <!-- 以下是使用wifi访问网络所需要的权限 -->
  14.     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
  15.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  16.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  17.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  18. </manifest>




阅读(689) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~