收听Android上的连接更改

时间:2018-06-22 17:08:54

标签: android

我正在使用Android应用程序,但我需要了解网络状态:离线,漫游,wifi,wifi热点,wifi直接,移动数据。 我已经创建了这个单例课程:

public class NetworkController {

private static final NetworkController networkController = new NetworkController();

private boolean peerConnected;
private ConnectionMode connectionMode;

public enum ConnectionMode {
    OFFLINE,
    MOBILE_DATA,
    ROAMING,
    WIFI,
    WIFI_HOSPOT,
    WIFI_DIRECT
}

private NetworkController() {
    connectionMode = ConnectionMode.OFFLINE;
}

public static NetworkController getNetworkController() {
    return networkController;
}

public void setPeerConnected(boolean peerConnected) {
    this.peerConnected = peerConnected;
}

public boolean isPeerConnected() {
    return peerConnected;
}

public ConnectionMode getConnectionMode() {
    return connectionMode;
}

public void setConnectionMode(ConnectionMode connectionMode) {
    this.connectionMode = connectionMode;
}

public void updateNetworkStatus() {
    connectionMode = ConnectionMode.OFFLINE;
    if (isRoaming()) {
        connectionMode = ConnectionMode.ROAMING;
    } else if (isConnectedToWiFi()) {
        connectionMode = ConnectionMode.WIFI;
        if (isHotspot())
            connectionMode = ConnectionMode.WIFI_HOSPOT;
        if (isWiFiDirect())
            connectionMode = ConnectionMode.WIFI_DIRECT;
    } else if (isMobileData()) {
        connectionMode = ConnectionMode.MOBILE_DATA;
    }
}

public boolean isRoaming() {
    try {
        ConnectivityManager connectivityManager = (ConnectivityManager) KeweltaApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = null;
        if (connectivityManager != null) {
            netInfo = connectivityManager.getActiveNetworkInfo();
        }
        if (netInfo != null) {
            return netInfo.isRoaming();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

public boolean isConnectedToWiFi() {
    try {
        ConnectivityManager connectivityManager = (ConnectivityManager) KeweltaApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = null;
        if (connectivityManager != null) {
            netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        }
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

public boolean isHotspot() {
    boolean isWifiAPenabled = false;
    WifiManager wifi = (WifiManager) KeweltaApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = new Method[0];
    if (wifi != null) {
        wmMethods = wifi.getClass().getDeclaredMethods();
    }
    for (Method method : wmMethods) {
        if (method.getName().equals("isWifiApEnabled")) {
            try {
                isWifiAPenabled = (Boolean) method.invoke(wifi);
            } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    return isWifiAPenabled;
}

public boolean isWiFiDirect() {
    return peerConnected;
}

public boolean isMobileData() {
    ConnectivityManager cm = (ConnectivityManager) KeweltaApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = null;
    if (cm != null) {
        activeNetwork = cm.getActiveNetworkInfo();
    }
    return activeNetwork != null && activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
}
}

我制作了此广播接收器:

public class ConnectionReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
        NetworkController.getNetworkController().updateNetworkStatus();
    }
}
}

我添加到了清单

<receiver android:name=".server.broadcast.ConnectionReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
</receiver>

“更新网络状态”方法仅设置“ OFFLINE”或“ WIFI”,我需要了解其他连接类型。另外,Android Studio Lint检查说:“针对N或更高版本的应用,不建议为android.net.conn.CONNECTIVITY_CHANGE声明广播接收器。通常,应用不应依赖于此广播,而应使用JobScheduler或GCMNetworkManager。” 我确定这意味着什么,我查看了stackoverflow,以寻找我的问题的最新解决方案,但我发现了三年前的帖子。 但这不是我现在的问题,我的问题是未检测到连接类型。方法“ isMobileData”,“ isHotspot”和“ isRoaming”不起作用。

1 个答案:

答案 0 :(得分:1)

这意味着您不能隐式地从清单中声明的​​广播接收器中监听CONNECTIVITY_CHANGE。在N及更高版本中,您的广播接收器将不会被呼叫。

如果您需要状态,则需要创建一个前台服务并在该服务中注册一个广播接收器以侦听连接更改