Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android 管理wifi,androidwifi

android 管理wifi,androidwifi

編輯:關於android開發

android 管理wifi,androidwifi


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/openwifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開啟WiFi" />

    <Button
        android:id="@+id/closewifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="關閉WiFi" />

    <Button
        android:id="@+id/wifistate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WiFi狀態" />

</LinearLayout>
package com.example.yanlei.wifi;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btnOpen = null;
    private Button btnClose = null;
    private Button btnState = null;

    private WifiManager wifi = null;

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

        btnOpen = (Button) super.findViewById(R.id.openwifi);
        btnClose = (Button) super.findViewById(R.id.closewifi);
        btnState = (Button) super.findViewById(R.id.wifistate);

        //獲取Wifi服務
        wifi = (WifiManager) super.getSystemService(Context.WIFI_SERVICE);

        //開啟
        btnOpen.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                wifi.setWifiEnabled(true);
                Toast.makeText(getApplicationContext(), "開啟Wifi服務!", Toast.LENGTH_LONG).show();
            }
        });

        //關閉
        btnClose.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                wifi.setWifiEnabled(false);
                Toast.makeText(getApplicationContext(), "關閉Wifi服務!", Toast.LENGTH_LONG).show();
            }
        });

        //狀態
        btnState.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Wifi狀態:" + String.valueOf(wifi.getWifiState()), Toast.LENGTH_LONG).show();
            }
        });

    }
}

權限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yanlei.wifi" >
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

參考:http://blog.csdn.net/jianghuiquan/article/details/8641123

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved