APK:广播监听wifi的状态

Android7.0 https://www.cnblogs.com/fengfenghuifei/p/8404743.html 

一、wifi网卡状态、操作网卡的权限

1.1、wifi网卡状态
a.WIFI_STATE_DISABLED:WIFI网卡不可用
b.WIFI_STATE_DISABLING:WIFI正在关闭
c.WIFI_STATE_ENABLED:WIFI网卡可用
d.WIFI_STATE_ENABLING:WIFI网卡正在打开
e.WIFI_STATE_UNKNOWN:未知网卡状态

二、代码

 2.1、AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gatsby.wifienable">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_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:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <data
                    android:host="akm.app"
                    android:pathPrefix="/openwith"
                    android:scheme="myapp" />
            </intent-filter>
        </activity>

        <receiver android:name=".MyBroadcast">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

 2.2、MyBroadcast

package com.gatsby.wifienable;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;

public class MyBroadcast extends BroadcastReceiver {

    Context mContext;
    WifiManager mWifiManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.mContext = context;
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.d("gatsby", "wifiState->" + wifiState);

            //wifi 开关
            mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            mWifiManager.setWifiEnabled(false);

            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    Log.d("gatsby", " WIFI_STATE_DISABLED " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    Log.d("gatsby", " WIFI_STATE_DISABLING " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_ENABLED:
                    Log.d("gatsby", " WIFI_STATE_ENABLED " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    Log.d("gatsby", " WIFI_STATE_ENABLING " + wifiState);
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN:
                    Log.d("gatsby", " WIFI_STATE_UNKNOWN " + wifiState);
                    break;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/crushgirl/p/13181624.html