打开WIFI

      其实打开WIFI和打开蓝牙差不多,只不过是在得到周围的WIFI于得到周围的蓝牙可不一样了,这里会用到ScanfReslut。

代码其实没有什么多少:

   首先是几个写着控件的布局文件:

    

<?xml version="1.0" encoding="UTF-8"?>
-<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello"/><Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/open" android:onClick="onAction" android:id="@+id/open"/><Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/close" android:onClick="onAction" android:id="@+id/close"/><Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/check" android:onClick="onAction" android:id="@+id/check"/><ListView android:layout_height="match_parent" android:layout_width="fill_parent" android:id="@+id/share_bluetooth"> </ListView></LinearLayout>

接下来就是他的java类:

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啦!

一切只是为了充实自己!!stay hungry and stay foolish!!
原文地址:https://www.cnblogs.com/Catherine-Brain/p/3555190.html