繁体   English   中英

启用WiFi时,广播接收器能否接收移动数据CONNECTIVITY_CHANGE通知?

[英]Can a Broadcast Receiver receive a Mobile Data CONNECTIVITY_CHANGE notification while WiFi is enabled?

对于有关广播接收器和移动数据连接的主题的许多主题,博客,示例和教程,我还没有看到这个问题的提出或回答。

我相信,根据对我的一个应用程序进行的试验,该问题的答案是完全不同的,在启用WiFi的情况下,监听移动数据CONNECTIVITY_CHANGE的广播接收器在该事件发生时不会收到广播通知。 如果我错了并且错过了一些东西,请告诉我。

我的应用程序是具有两个类的主屏幕窗口小部件,ActiveMobileData是AppWidgetProvider,而ConnectivityChangeReceiver是BroadcastReceiver。 AppWidgetProvider类是我的第一款应用程序,我今年初将其组合在一起,主要是从一本书,StackOverflow和各种博客等上广泛使用的代码中获得的。没有应用程序只是主屏幕小部件。 只需在红色和绿色之间切换主屏幕图标即可指示当前的移动数据状态。 在大约100个用户的支持下,它已经完美运行了几个月。

我决定添加BroadcastReceiver以从“设置”中获得点击。 此代码也很简单-它确定移动数据的当前状态,并使用AppWidgetProvider设置的全局布尔变量来确定主屏幕图标是红色还是绿色。 然后,只需确保图标颜色与移动数据状态匹配即可。

除了启用WiFi并不会收到通知外,其他所有操作均有效。 如果有解决此限制的方法,我将不胜感激。

以下是小部件的代码,然后是接收者的代码。 我省略了一些细节以使其简短。 iconEnabled是共享的全局布尔变量。

public class ActiveMobileData extends AppWidgetProvider {
static boolean iconEnabled;
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction() != null)
        super.onReceive(context, intent);
    else {
        context.startService(new Intent(context, ToggleService.class));
    }
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]appWidgetIds) {
    context.startService(new Intent(context, ToggleService.class));
}
public static class ToggleService extends IntentService {
    public ToggleService() {
        super("ActiveMobileData$ToggleService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        ComponentName cn = new ComponentName(this, ActiveMobileData.class);
        AppWidgetManager mgr = AppWidgetManager.getInstance(this);
        mgr.updateAppWidget(cn, buildUpdate(this));
    }
    private RemoteViews buildUpdate(Context context) {
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        if (!isMobileDataEnabled(getApplicationContext())) {
            updateViews.setImageViewResource(R.id.mobileDataState, R.mipmap.ic_launcher_g);
            enableMobileData(getApplicationContext(), true);
            iconEnabled = true;
        } else {
            updateViews.setImageViewResource(R.id.mobileDataState, R.mipmap.ic_launcher_r);
            enableMobileData(getApplicationContext(), false);
            iconEnabled = false;
        }
        Intent i = new Intent(this, ActiveMobileData.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        updateViews.setOnClickPendingIntent(R.id.mobileDataState, pi);
        return updateViews;
    }
    public boolean isMobileDataEnabled(Context context) {
        // ... the code here is the one that uses Java reflection
    }
    private void enableMobileData(Context context, boolean enabled) {
        // ... the code here is the one that uses Java reflection
    }

  } // public static class ToggleService
} // public class ActiveMobileData

以下是BroadcastReceiver的代码...

public class ConnectivityChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive (Context context, Intent intent) {
        handleIntent(context);
    }
    protected void handleIntent(Context context) {
        ComponentName cn = new ComponentName(context, ActiveMobileData.class);
        AppWidgetManager mgr = AppWidgetManager.getInstance(context);
        mgr.updateAppWidget(cn, buildUpdate(context));
    }
    private RemoteViews buildUpdate(Context context) {
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        if (!ActiveMobileData.iconEnabled && isMobileDataEnabled(context)) {
            ActiveMobileData.iconEnabled = true;
            updateViews.setImageViewResource(R.id.mobileDataState, R.mipmap.ic_launcher_g);
            Intent i = new Intent(context, ActiveMobileData.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            updateViews.setOnClickPendingIntent(R.id.mobileDataState, pi);
        } else
        if (ActiveMobileData.iconEnabled && !isMobileDataEnabled(context)) {
            ActiveMobileData.iconEnabled = false;
            updateViews.setImageViewResource(R.id.mobileDataState, R.mipmap.ic_launcher_r);
            Intent i = new Intent(context, ActiveMobileData.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            updateViews.setOnClickPendingIntent(R.id.mobileDataState, pi);
        }
        return updateViews;
    }
    private boolean isMobileDataEnabled(Context context) {
       // ... Identical code to that in the AppWidgetProvider
    }
} // class ConnectivityChangeReceiver

我暂时不会知道,但我可以为您指出2个最好的地方。

最好的选择是详细研究JavaDoc for ConnectivityManager.html#CONNECTIVITY_ACTION ,然后在GrepCode上在线查看ConnectivityManager源代码

特别是源代码中的注释通常具有非常有用的信息,而其他地方则没有。


更新:

再次阅读CONNECTIVITY_ACTION的javadoc后,我相信您是正确的,因为它说A change in network connectivity has occurred. A default connection has either been established or lost. A change in network connectivity has occurred. A default connection has either been established or lost. 注意:默认连接不是“ A连接”。 因此,仅当“默认”更改时才会启动。 因此,如果您在WIFI上丢失3g / 4g / etc,那么我认为这不会启动。

但是,有“您可以”做的事情……(但仅当您的小部件正在运行时)(实际上,我不是100%地确定“小配件”可以做到这一点……b / c我通常在教书services / AIDL / ContentProviders / etc(又称平台内的“后端”东西),但是您可以在小部件上放置一个“刷新”按钮,该查询可以查询到GET ALL NETWORKS ,然后解析所有数据并显示哪些网络是“活跃等

也有选择。 您可以为广播接收者制定待处理的意图(我建议仅使用1个BR,并具有不同的有效负载,以便您可以对收到的通知进行排序),然后将每个待处理的意图注册为ConnectivityManager的回调 ,以便每当存在“与” NetworkRequest “匹配”的“网络”时通知它。 这将至少在他们“还活着”时通知您...

(下一个想法可能需要您使用单独的线程来提供服务,以防止ANR)

现在,当他们“死亡” ...时,您“可以”建立一个TCP连接,看看它何时死亡...(不是“好”,而可能只是“可行”的选择)(如果您“慷慨”地尝试而不唤醒手机,则对电池的影响可能很小)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM