现在的位置: 首页 > 综合 > 正文

操作WIFI

2018年03月31日 ⁄ 综合 ⁄ 共 3730字 ⁄ 字号 评论关闭

 

需要在真机上使用才有更好的效果

 

 

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <TextView

      android:id="@+id/msg"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:layout_marginTop="8dp"

      android:gravity="center_horizontal"

      android:textSize="20dp"

      android:textColor="#ffffff"/>

  <Button

      android:id="@+id/open"

      android:layout_marginTop="8dp"

      android:layout_width="80dp"

      android:layout_height="40dp"

      android:background="#3399ff"

      android:textColor="#ffffff"

      android:text="打开WIFI"/>

  <Button

      android:id="@+id/close"

      android:layout_marginTop="8dp"

      android:layout_width="80dp"

      android:layout_height="40dp"

      android:background="#3399ff"

      android:textColor="#ffffff"

      android:text="关闭WIFI"/>

  <Button

      android:id="@+id/check"

      android:layout_marginTop="8dp"

      android:layout_width="80dp"

      android:layout_height="40dp"

      android:background="#3399ff"

      android:textColor="#ffffff"

      android:text="检查WIFI"/>

</LinearLayout>

 

 

 

 

 

 

在MyWifiDemo.java中:

 

package com.li.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.TextView;

 

public class MyWifiDemo extends Activity {

  private Button open = null ;

  private Button close = null ;

  private Button check = null ;

  private TextView msg = null ;

  private WifiManager wifiManager = null ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.open = (Button) super.findViewById(R.id.open) ;

     this.close = (Button) super.findViewById(R.id.close) ;

     this.check = (Button) super.findViewById(R.id.check) ;

     this.msg = (TextView) super.findViewById(R.id.msg) ;

     this.wifiManager = (WifiManager) super

         .getSystemService(Context.WIFI_SERVICE);

     this.open.setOnClickListener(new OnClickListener(){

 

       public void onClick(View v) {

         MyWifiDemo.this.wifiManager.setWifiEnabled(true) ;  // 启用wifi

         MyWifiDemo.this.msg.setText("打开WIFI状态:" + MyWifiDemo.this.wifiManager.getWifiState()) ;

       }

      

     }) ;

     this.close.setOnClickListener(new OnClickListener(){

 

       public void onClick(View v) {

         MyWifiDemo.this.wifiManager.setWifiEnabled(false) ;  // 启用wifi

         MyWifiDemo.this.msg.setText("关闭WIFI状态:" + MyWifiDemo.this.wifiManager.getWifiState()) ;

       }

      

     }) ;

     this.check.setOnClickListener(new OnClickListener(){

 

       public void onClick(View v) {

         MyWifiDemo.this.msg.setText("检查WIFI状态:" + MyWifiDemo.this.wifiManager.getWifiState()) ;

       }

      

     }) ;

  }

}

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.wifi"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyWifiDemo"

            android:label="@string/title_activity_my_wifi_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

 

【上篇】
【下篇】

抱歉!评论已关闭.