简体   繁体   English

Android服务,每x分钟上传x分钟数据

[英]Android Service for uploading data for x minute in every x minute

I have 2 service running in background. 我有2个服务在后台运行。 One that will constantly collect location data and another to send data to server that will be start by AlarmManager every 10 minutes. 一个将不断收集位置数据,另一个将数据发送到服务器,警报管理器将每10分钟启动一次。

In the service that send data to server, I have a method that will post data (50 rows from database) in onStartCommand() . 在将数据发送到服务器的服务中,我有一个方法将在onStartCommand()发布数据(数据库中的50行onStartCommand() I'd like to have the service execute the the post data method continuously for up to 5 minutes. 我想让服务连续执行post data方法长达5分钟。

I have tried to use a while loop to execute the method continuously but it will show message that app is not responding. 我尝试使用while循环连续执行该方法,但是它将显示消息应用程序未响应。 I think that the while loop is executing the method in service non-stop in Main thread causing the activity to crash. 我认为while循环正在Main线程中的服务不间断地执行该方法,从而导致活动崩溃。

Is there any better way to execute a method continuously in a service while to crashing activity on the main thread? 有什么更好的方法可以在服务中连续执行方法,同时使主线程上的活动崩溃?

I have 2 service running in background. 我有2个服务在后台运行。 One that will constantly collect location data and another to send data to server that will be start by AlarmManager every 10 minutes. 一个将不断收集位置数据,另一个将数据发送到服务器,警报管理器将每10分钟启动一次。

This will be terrible for battery life, as you will need to keep the CPU constantly powered on via a WakeLock to "constantly collect location data". 这对于电池寿命将是可怕的,因为您将需要通过WakeLock保持CPU持续通电以“不断地收集位置数据”。

In the service that send data to server, I have a method that will post data (50 rows from database) in onStartCommand(). 在将数据发送到服务器的服务中,我有一个方法将在onStartCommand()中发布数据(数据库中的50行)。 I'd like to have the service execute the the post data method continuously for up to 5 minutes. 我想让服务连续执行post data方法长达5分钟。

This will be even worse for battery life, as now you will keep the WiFi or mobile radios in a high-power state continuously for up to 5 minutes. 这对于电池寿命会变得更糟,因为现在您将使WiFi或移动无线电保持高功率状态持续长达5分钟。

Is there any better way to execute a method continuously in a service while to crashing activity on the main thread? 有什么更好的方法可以在服务中连续执行方法,同时使主线程上的活动崩溃?

Fork a background thread. 分叉一个后台线程。

You need to do your work on another thread. 您需要在另一个线程上进行工作。

The service helps control the lifecycle outside of the UI, but it is still on the MainThread. 该服务有助于在UI外部控制生命周期,但是它仍位于MainThread上。

It is called a "background service" as it has a background lifecycle which is different to you Activity lifecycle. 之所以称为“后台服务”,是因为它具有与您的活动生命周期不同的后台生命周期。

Simplest example is spawn an ASyncTask from your background Service or better use an IntentService . 最简单的示例是从后台Service生成ASyncTask或更好地使用IntentService An IntentService will automatically control your use of threads. IntentService将自动控制您对线程的使用。

Here is the official documentation for Intent Service thread work: 这是Intent Service线程工作的正式文档:

http://developer.android.com/training/run-background-service/send-request.html http://developer.android.com/training/run-background-service/send-request.html

And here are the API docs for what I discussed http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/app/IntentService.html 以下是我讨论过的内容的API文档: http: //developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/app/IntentService.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM