当检测到互联网连接下载数据互联网、检测到、数据

由网友(梦毁少年)分享简介:我想下载一些数据,包括JSON和图像(二进制数据)每当因特网的连接可用。我有一个完整的工作code,可以做到这一点。问题是,我已经写了code在自定义应用程序类。这将下载并保存locally.But当应用程序启动的互联网连接可能无法在那个时间点。 所以,我想要做的就是发送请求并下载数据时,只要有Internet连接,检...

我想下载一些数据,包括JSON和图像(二进制数据)每当因特网的连接可用。

我有一个完整的工作code,可以做到这一点。问题是,我已经写了code在自定义应用程序类。这将下载并保存locally.But当应用程序启动的互联网连接可能无法在那个时间点。

所以,我想要做的就是发送请求并下载数据时,只要有Internet连接,检测和保存它的内容提供商在本地使用这些数据。而应用程序将工作,即使没有连接到互联网。

我想用广播接收器来下载相同的。但是,那么这将是一个广播接收器下载数据任务繁重。所以,现在我所找出是我可以检查出互联网连接的广播接收器,我会从其他组件下载数据。这是这样做的最佳做法是什么?

广播接收机处于周围,即使应用程序是不是?

解决方案   

这是这样做的最佳做法是什么?

最好的办法是使用的的 IntentService 的 。

这是您的BroadcastReceiver启动此服务。通过的的 IntentService 的 ,所有的请求都在一个单一的工作线程处理 - 他们可能只要必要时采取(并不会阻止应用程序的主循环),所以不用担心你的任务是如何长。

  

广播接收机处于周围,即使应用程序是不是?

是的,如果他们是通过清单登记。

有用资源

创建一个后台服务 IntentService基础

I want to download some data including json and image(binary data) whenever internet connectivity is available.

以互联网行业为背景下的数据分析通识 上

I have a full working code which can do that. The problem is I have written that code in custom application class. which downloads it and saves locally.But when app is launched the internet connectivity may not be available at that point of time.

So what I want to do is send the request and download the data whenever internet connectivity is detected and use that data by saving it in content provider locally. And the app will work even without an internet connection.

I am thinking of using broadcast receiver to download the same. But then it will be heavy task for a broadcast receiver to download the data. So now what I am finding out is I can check out internet connectivity in broadcast receiver and I will download the data from some other component. Which is the best practice to do so?

Is broadcast receiver stays around even when application is not on?

解决方案

Which is the best practice to do so?

Best way is to use IntentService.

Start this service from your BroadcastReceiver. Via IntentService, all requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), so no worry about how long your task is.

Is broadcast receiver stays around even when application is not on?

Yes, if they are registered via manifest.

Helpful resources are

Creating a Background Service IntentService Basics

阅读全文

相关推荐

最新文章