zoukankan      html  css  js  c++  java
  • android 管理wifi

    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

  • 相关阅读:
    select option 下拉多选单选bootstrap插件使用总结
    bootstrap-dialog的使用
    display的table和cell外加table-layout:fixed等分布局,外加换行,word-wrap:break-word
    css样式实现字体删除线效果
    递归实现遍历二叉树
    童晶老师的游戏开发课程作业--实时时钟的实现
    张宵 20201120-1 每周例行报告
    张宵 20201112-1 每周例行汇报
    20201105-1 每周例行报告
    张宵 20201029-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5224832.html
Copyright © 2011-2022 走看看