服务延迟动作,直到网络连接可用动作、网络

由网友(こ珴与影子孤独终老う)分享简介:在我的应用程序,我想提供一个动作,这将在后台定期执行。所以,我用的是AlarmManager,启动一个IntentService。棘手的部分是,这种背景下的动作需要互联网连接。所以我试图用一个激活锁定这似乎并没有强制执行的连接,当设备被锁定。然后我想到了只要需要的广播接收注册一个BroadcastReceiver来监听...

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

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

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

我的code看起来是这样的:

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

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

感谢。

解决方案   

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

是的。这将住了几微秒。

  调研Redis高可用两种方案

和因此,我怎么能实现的服务,延缓它的工作,直到一个网络连接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.

阅读全文

相关推荐

最新文章