暂停服务直至建立互联网连接

发布于 2025-01-04 15:16:03 字数 160 浏览 4 评论 0原文

我有一项在启动完成后运行的服务。此服务需要互联网连接。等待设备连接到互联网的最佳做法是什么?手机有wifi真的不重要。

我当前的解决方案涉及一个 while 循环,该循环仅检查 ConnectivityManager 直到其中一个网络可用,但这感觉很粗俗。

有更好的方法吗?

I have a service that runs upon boot completion. This service requires internet connectivity. What's the best practice for waiting for the device to connect to the internet? Mobile of wifi doesn't really matter.

My current solution involves a while loop that just checks ConnectivityManager until one of the networks becomes available, but this feels vulgar.

Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠我空喜 2025-01-11 15:16:03

但这感觉很粗俗

确实很粗俗:D

  1. 你的接收器唤醒你的唤醒意图服务(可能是一个简单的意图服务就可以了,因为手机在启动时不会休眠)
  2. 服务注册一个接收器以进行连接
  3. 服务等待 CountDownLatch
  4. 接收器唤醒服务当 wifi 连接时

骨架代码:https://stackoverflow.com/a/19968708/281545 - 你的情况更简单,因为你不必唤醒wifi,否则(包括需要很长时间和无线电/CPU 睡眠的情况 - 在这种情况下,简单的意图服务将不起作用)在 2 到 3 之间,您将需要:

2a.服务获取wifi锁
2b.服务调用 reconnect()reassociate() 以及所需的任何内容(这可能是特定于设备的)

but this feels vulgar

Indeed :D

  1. Your receiver wakes your wakeful intent service (probably a simple intent service would do, as the phone does not sleep while booting AFAIK)
  2. service registers a receiver for connectivity
  3. service waits on a CountDownLatch
  4. the receiver wakes the service up when the wifi is connected

Skeleton code : https://stackoverflow.com/a/19968708/281545 - your case is simpler as you do not have to wake the wifi, hold wifi locks etc. Otherwise (including the case this takes long and radios/CPU sleep - in which case a simple intent service won't do) between 2 and 3 you would need to :

2a. service acquires a wifi lock
2b. service calls reconnect(), reassociate() and whatever is needed (this may be device specific)

梦晓ヶ微光ヅ倾城 2025-01-11 15:16:03

您可以使用 BroadcastReceiver:

private class ConnectionMonitor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
            return;
        boolean noConnectivity = intent.getBooleanExtra(
            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo aNetworkInfo = (NetworkInfo) intent
            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (!noConnectivity) {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // start your service stuff here
            }
        } else {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // stop your service stuff here
            }
        }
    }
}

然后,在代码中的某个位置实例化:

ConnectionMonitor connectionMonitor = new ConnectionMonitor();
registerReceiver(connectionMonitor, intentFilter);

注意:此代码来自 检测3G或Wifi网络恢复

You could use a BroadcastReceiver:

private class ConnectionMonitor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
            return;
        boolean noConnectivity = intent.getBooleanExtra(
            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo aNetworkInfo = (NetworkInfo) intent
            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (!noConnectivity) {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // start your service stuff here
            }
        } else {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // stop your service stuff here
            }
        }
    }
}

Then, you instantiate somewhere in your code:

ConnectionMonitor connectionMonitor = new ConnectionMonitor();
registerReceiver(connectionMonitor, intentFilter);

Note: this code comes from Detect 3G or Wifi Network restoration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文