【问题标题】:Can a Broadcast Receiver receive a Mobile Data CONNECTIVITY_CHANGE notification while WiFi is enabled?启用 WiFi 时,广播接收器能否接收移动数据 CONNECTIVITY_CHANGE 通知?
【发布时间】:2015-12-24 21:08:02
【问题描述】:

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

我相信,基于对我的一个应用程序的试验,这个问题的答案是一个明显的否定,即当启用 WiFi 时,监听移动数据 CONNECTIVITY_CHANGE 的广播接收器在该事件发生时不会收到广播通知.如果我错了并且遗漏了什么,请告诉我。

我的 App 是一个主屏幕 Widget,有两个类,ActiveMobileData 是 AppWidgetProvider,ConnectivityChangeReceiver 是 BroadcastReceiver。 AppWidgetProvider 类是我今年早些时候编写的第一个应用程序,主要来自一本书、StackOverflow 和各种博客等中广泛使用的代码。没有应用程序,只有主屏幕小部件。它只是在红色和绿色之间切换主屏幕图标,以指示当前的移动数据状态。它已经在大约 100 名用户中完美运行了几个月。

我决定添加广播接收器以从设置中获取点击。这段代码也很简单——它确定移动数据的当前状态,并使用 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

以下是广播接收器的代码...

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

【问题讨论】:

    标签: java android widget broadcast connectivity


    【解决方案1】:

    我不知道,但我可以为您指出 2 个最佳观看地点。

    最好的办法是详细查看JavaDoc for ConnectivityManager.html#CONNECTIVITY_ACTION,然后是source code for the ConnectivityManager that is online on GrepCode

    尤其是源代码中的 cmets 通常包含其他地方不存在的非常有用的信息。


    更新:

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

    但是,您“可以”做一些事情...(但仅在您的小部件运行时)(实际上,我不是 100% 确定“小部件”可以做到这一点... b/c 我通常与教学服务/AIDL/ContentProviders/etc(也就是平台内的“后端”内容)一起工作,但是您可以在您的小部件上放置一个“刷新”按钮,该按钮可以查询GET ALL NETWORKS,然后解析所有这些数据并显示哪个网络“活跃”等。

    还有一个选项。您可以为广播接收器创建待处理的意图(我建议只使用 1 个 BR 并具有不同的有效负载,以便您可以根据通知对它们进行排序)然后使用 ConnectivityManager 将每个待处理的意图注册为 a call back 以每当存在“匹配”NetworkRequest 的“网络”时通知它。这至少会在它们“复活”时通知您...

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

    现在当他们“死”时...... ' 尝试不唤醒手机,对电池的影响可能很小)

    【讨论】:

    • 我通过这些链接阅读了很多有趣的信息,但不幸的是我没有找到任何与我的任务相关的 cmets 等。我也确实探索了将 ContentObserver 与 android.provider.Settings.System.CONTENT_URI 一起使用的想法,以查看它是否会从设置中获取移动数据或任何更改,但这也没有成功。我不确定它是否可以通过 ContentObserver 获取,如果可以,那么 URI 是什么。如果有人对我有其他建议,我将不胜感激。
    • 更新,添加到答案中,因为评论太长了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多