服务延迟动作,直到网络连接可用 [英] Delay service action until network connection is available

查看:222
本文介绍了服务延迟动作,直到网络连接可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想提供一个动作,这将在后台定期执行。所以,我用的是AlarmManager,启动一个IntentService。

棘手的部分是,这种背景下的动作需要互联网连接。所以我试图用一个激活锁定这似乎并没有强制执行的连接,当设备被锁定。

然后我想到了只要需要的广播接收注册一个BroadcastReceiver来监听android.net.conn.CONNECTIVITY_CHANGE在服务启动时,立即注销它。

我的code看起来是这样的:

 公共类BackgroundService扩展IntentService {
    私有静态最终IntentFilter的过滤器=
        新的IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    私有静态NetworkStateChangedReceiver接收器=
        新NetworkStateChangedReceiver();    保护无效onHandleIntent(意向意图){
        registerReceiver(接收器,过滤器);
    }
}

我的问题是现在:请问这个接收器被破坏,只要该服务停止(因为它没有任何关系,只要没有连接可用)?
因此,我怎么能实现一个服务,直到网络连接availalbe延缓它的工作?

感谢。


解决方案

  

请问这个接收器被破坏,只要该服务停止(因为它没有任何关系,只要没有连接可用)?


是的。这将住了几微秒。


  

和因此,我怎么能实现的服务,延缓它的工作,直到一个网络连接availalbe?


使用 ConnectivityManager getActiveNetworkInfo(),看看是否有一个连接,如果没有,就退出 onHandleIntent(),等待下一次报警。

或者,注册您的 CONNECTIVITY_ACTION 接收器清单,并用它来安排和取消警报,所以警报才有效,而有一个连接。

有各种其他的策略,但是这两个应该给你一个起点。

In my app, I would like to offer an action, which shall be executed in background regularly. So I use the AlarmManager, which starts an IntentService.

The tricky part is, that this background action needs Internet connection. So I tried using a WakeLock which didn't seem to enforce a connection, when the device was locked.

Then I thought about registering a BroadcastReceiver to listen for "android.net.conn.CONNECTIVITY_CHANGE" when the service starts and immediately unregistering it, as soon as the desired broadcast is received.

My code looks something like this:

public class BackgroundService extends IntentService {
    private static final IntentFilter filter =
        new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    private static NetworkStateChangedReceiver receiver =
        new NetworkStateChangedReceiver();

    protected void onHandleIntent(Intent intent) {
        registerReceiver(receiver, filter);
    }
}

My question is now: Will this receiver be destroyed, as soon as the service stops (as it has nothing to do, as long as no connection is available)? And therefore, how can I realize a service, that delays it's work until a network connection is availalbe?

Thanks.

解决方案

Will this receiver be destroyed, as soon as the service stops (as it has nothing to do, as long as no connection is available)?

Yes. It will live for a few microseconds.

And therefore, how can I realize a service, that delays it's work until a network connection is availalbe?

Use ConnectivityManager and getActiveNetworkInfo(), see if there's a connection, and if not, just exit onHandleIntent() and wait for the next alarm.

Or, register your CONNECTIVITY_ACTION receiver in the manifest and use it to schedule and cancel your alarms, so the alarms are only active while there's a connection.

There are various other strategies, but those two should give you a starting point.

这篇关于服务延迟动作,直到网络连接可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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