使用IntentService下载数据 - 生命周期的变化 [英] Downloading data using IntentService - Lifecycle changes

查看:155
本文介绍了使用IntentService下载数据 - 生命周期的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

铊;博士如何知道什么时候一个 IntentService 已完成下载后返回到活动使用监听其结果广播接收器

Tl;dr How to know when an IntentService has finished downloading upon returning to the Activity which listens to its result using a BroadcastReceiver?

我要搬到实现数据下载到 IntentService S,并通知当任务使用完广播接收器秒。

I'm moving to implementing data downloading to IntentServices, and notifying when the task has finished using BroadcastReceivers.

我一般启动该服务在我的活动

I generally start the service in my Activity:

IntentFilter intentFilter = DownloadDataService.startDownloadData(this);
getLocalBroadcastManager().registerReceiver(mBroadcastReceiver, intentFilter);

这是启动服务部分

/**
 * Starts the service and returns an IntentFilter to subscribe a BroadcastReceiver on. 
 * When the task has finished, a broadcast for returned IntentFilter is sent,
 * containing downloaded data.
 */
public static IntentFilter startDownloadData(final Context context) {
    Intent intent = new Intent(context, DownloadDataService.class);
    intent.setAction(ACTION_DOWNLOAD_DATA);
    context.startService(intent);

    return new IntentFilter(ACTION_DOWNLOAD_DATA);
}

当然, onHandleIntent(意向)(简体):

@Override
protected void onHandleIntent(final Intent intent){
    Data data = downloadData();
    Intent intent = new Intent(ACTION_DOWNLOAD_DATA);
    intent.putExtra(DATA, data);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

这一切运作良好,我能保持状态在我的活动来知道例如改变方向后,我是否在等待下载的数据结果:

This all works well, and I can keep states in my Activity to know for example after an orientation change whether I was waiting for a download data result:

@Override
public void onResume(){
    super.onResume();
    if (mState == State.DOWNLOADING) {
        Log.v(TAG, "Registering receiver for existing download data");
        IntentFilter intentFilter = DownloadDataService.getIntentFilter();
        getLocalBroadcastManager().registerReceiver(mBroadcastReceiver, intentFilter);
    }
}

太好了,现在我也可以处理方向变化。只有一个问题留给:

Great, now I can also handle orientation changes. Only one problem left:


  • 活动启动 DownloadDataService

  • 用户从活动移开

  • DownloadDataService 广播其完成的消息(这不是由活动收到由于 unregisterReceiver 的onStop()

  • 用户移动回活动

  • 活动仍然认为它是等待 DownloadDataService ,和什么也不做。

  • Activity starts the DownloadDataService
  • User moves away from the Activity
  • DownloadDataService broadcasts its done message (which is not received by the Activity due to unregisterReceiver in onStop())
  • User moves back into the Activity
  • Activity still thinks it's waiting for the DownloadDataService, and does nothing.

我该如何弥补呢?

请注意,我没有这样用于存储下载的数据数据库的任何持久性。在活动从检索数据广播意图

Note that I do not have any persistence like databases for storing the downloaded data. The Activity retrieves the data from the broadcasted Intent.

注2:有这个答案来的问题如何知道是否服务正在运行。虽然这可能会奏效,但明确表示,该方法是调试或实施服务管理型的用户界面

Note #2: There is this answer to the question of how to know whether a Service is running. Although this might work, it is explicitly stated that that method is for debugging or implementing service management type user interfaces.

推荐答案

我是不是真的通过使用共享preferences,静态变量和其他哈克的解决方案信服。

I wasn't really convinced by using SharedPreferences, static variables and other 'hacky' solutions.

我也不过发现你可以提供一个 ResultReceiver - 这是parcelable - 你可以用它来通知你的任务就完成了。它接收一个处理程序来指定线程结果的处理上。

I did find however that you can supply a ResultReceiver - which is parcelable - which you can use to notify your task is finished. It receives a Handler to specify the thread the result is handled on.

这样做的好处是,你可以在的onSaveInstanceState 保存 ResultReceiver 。使用一些聪明的你一定可以使这项工作。我创建了有利于这种战术的实验库: https://github.com/nhaarman/Ergo

The advantage of this, is that you can save the ResultReceiver during onSaveInstanceState. Using some clever tricks you can certainly make this work. I have created an experimental library which facilitates this tactic: https://github.com/nhaarman/Ergo

这篇关于使用IntentService下载数据 - 生命周期的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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