是支持Wi-Fi时,不要接收移动状态状态更改事件 [英] Do not receive mobile state state change event when wi-fi is enabled

查看:287
本文介绍了是支持Wi-Fi时,不要接收移动状态状态更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在移动数据被启用/禁用得到通知。为了这个目的,我使用了BroadcastReceiver和注册ConnectivityManager.CONNECTIVITY_ACTION事件。但是,只有当Wi-Fi已禁用该事件被触发。当我启用Wi-Fi我停下得到任何事件时启用/禁用移动数据。

I need to get notified when mobile data gets enabled/disabled. For that purpose I am using BroadcastReceiver and register to ConnectivityManager.CONNECTIVITY_ACTION event. However that event is triggered only when Wi-Fi is disabled. As soon as I enable Wi-Fi I stop getting any event when enabling/disabling mobile data.

任何想法?
我需要考虑接收Wi-Fi状态的移动数据状态更改事件。

Any ideas? I need to receive mobile data state change event regardless of wi-fi state.

推荐答案

我测试了两种动作接收器。

I tested two action receiver.

1

ConnectivityManager.CONNECTIVITY_ACTION;

如果无线connnected它不能接收启用或禁用移动数据。

It cannot receive mobile data enabled or disabled if wifi connnected.

2

TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED = "android.intent.action.ANY_DATA_STATE" 

(它是隐藏的)

它可以接收无线网络是否连接禁用移动数据。,并启用移动数据无法接收。

It can receive mobile data disabled if wifi connected., and cannot receive mobile data enabled.

我的测试设备是三星Galaxy S4迷你LTE韩国模型(SHV-E370K)不是全局模型(GT-I9195)

My test device is Samsung Galaxy S4 mini LTE Korean Model(SHV-E370K) not Global Model (GT-I9195)

==========================================

==========================================

如果无线连接时,系统不会调用dataEnabled(因为不需要移动数据)。

If wifi connected, system does not call dataEnabled (because does not need mobile data).

所以不能接收移动启用状态(实际上,移动数据不enbaled)

So cannot receive mobile enabled state (Actually, mobile data is NOT enbaled)

我决定安排定时器(周期= 10000ms),并检查 getMobileDataEnabled()

I decided to schedule timer (period = 10000ms) and check getMobileDataEnabled().

private Method connectivityManager_getMobileDataEnabled = null;
private Method getConnectivityManager_getMobileDataEnabled() throws NoSuchMethodException {

    if (connectivityManager_getMobileDataEnabled == null) {
        connectivityManager_getMobileDataEnabled = ConnectivityManager.class.getMethod(
                "getMobileDataEnabled",
                new Class[0]);
    }

    return connectivityManager_getMobileDataEnabled;
}

public boolean getMobileDataEnabled() 
        throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    Method getMobileDataEnabled = getConnectivityManager_getMobileDataEnabled();
    getMobileDataEnabled.setAccessible(true);
    return (Boolean) getMobileDataEnabled.invoke(mConnectivityManager);
}

这篇关于是支持Wi-Fi时,不要接收移动状态状态更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆