【Android學習筆記】WIFI控制(開啟、關閉)


WIFI的控制,主要是透過WifiManager來管理WIFI
詳細資訊可以參考WifiManager | Android Developers

此篇文章主要目的是建立兩個Button去控制WiFi的開啟與關閉


在此附上完整程式碼:

1. MainActivity.java 程式碼
package tw.app.ryan.wifimanagerexample;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
    private WifiManager mWifiMngr;//管理並控制Wifi
    private Button btnWifiOn;//開啟wifi
    private Button btnWifiOff;//關閉wifi

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //取得介面資源
        btnWifiOn = (Button)findViewById(R.id.btnWifiOn);
        btnWifiOff = (Button)findViewById(R.id.btnWifiOff);
        //設定Wifi裝置
        mWifiMngr = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);//取得WifiManager
        //設定按鈕功能
        // 開啟wifi
        btnWifiOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //若wifi狀態為關閉則將它開啟
                if (!mWifiMngr.isWifiEnabled()) {
                    mWifiMngr.setWifiEnabled(true);
                }
            }
        });

        // 關閉wifi
        btnWifiOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //若wifi狀態為開啟則將它關閉
                if (mWifiMngr.isWifiEnabled()) {
                    mWifiMngr.setWifiEnabled(false);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

2. main.xml(Layout)程式碼

<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
    <textview android:id="@+id/txtTitle" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="WifiManager" android:textappearance="?android:attr/textAppearanceLarge">

    <linearlayout android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content">

        <button android:id="@+id/btnWifiOn" android:layout_height="wrap_content" android:layout_width="180dp" android:text="開啟Wifi">
        </button>

        <button android:id="@+id/btnWifiOff" android:layout_height="wrap_content" android:layout_width="180dp" android:text="關閉Wifi">
        </button>
    </linearlayout>
</textview></linearlayout>
3. AndroidManifest.xml
記得要在AndroidManifest檔案裡面增加uses-permission給他:
<manifest package="tw.app.ryan.wifimanagerexample" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
        <activity android:label="@string/app_name" android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN">

                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </activity>
    </application>
</manifest>


.執行畫面

5.GitHub 程式碼
連結在此

沒有留言:

張貼留言