在后台更新数据定期(Android版) [英] Update data in Background Periodically (Android)

查看:182
本文介绍了在后台更新数据定期(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的主要目的是,当按钮为pressed:

The main objective of my app is, when a button is pressed:


  1. 从传感器收集数据

  2. 在数据库中存储的数据

  3. 重复定期(每X秒)

我想有它在后台完成,如果主要活动被破坏,后台进程是仍在运行。所以,如果你删除的活动,我希望它仍在运行。

I want to have it done in background, and if the Main Activity is destroyed, the background progress to be still running. So if you delete the activity, I want it still running.

我现在在做什么,是有一个 AlarmManager 意图服务,设置定期记录数据,但每当我摧毁活动,因为它们是相关的,我的应用程序崩溃。

What I am doing right now, is having an AlarmManager with an Intent Service, to set the periodic recording of the Data, but whenever I destroy the Activity, as they are related, my app crashes.

我知道有不同的运行后台进程,但没有在煤矿适合:

I know there are different to run a background process, but none fits in mine:


  • 服务:取决于MainThread,因此,如果在MainActivity被破坏,
    崩溃

  • 意图服务:现在使用的人,但只要是
    有关AlarmManager,即有关MainActivity,当它是
    破坏,死机。

  • 的AsyncTask :这一个问题,就是它
    应该做一个任务的背景和它完成,但我
    真正需要的是,直到用户表示,停止定期去做。所以
    它不是一次性的事情了。

  • Service: Depends on MainThread, so if the MainActivity is destroyed, crashes
  • Intent Service: The one using right now, but as far as it is related to AlarmManager, that is related to MainActivity, when it is destroyed, crashes.
  • AsyncTask: The problem of this one, is that it is supposed to do one task in background and it finishes, but what I really want is to do it periodically until the user says to stop. So it's not a matter of one time thing.

有没有其他的办法有一个后台服务?
可以在任何的前面所说用于我的目的是什么?
你怎么建议我做呢?

Is there any other way to have a background service? Can any of the stated before be used for my purpose? How do you recommend me to do it?

谢谢您的回答。

推荐答案

尝试使用单独的进程来启动服务<一个href=\"http://stackoverflow.com/questions/7142921/usage-of-androidprocess\"><$c$c>android:process=\":servicename\"在AndroidManifest.xml中。如果服务在另一个进程中运行,它不会与活性杀死。

Try to start service in separate process using android:process=":servicename" in AndroidManifest.xml. If service is running in another process, it wouldn't be killed with activity.

这篇关于在后台更新数据定期(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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