saving data update service for an android app

I am building an android app for fetching internet data and rendering it as a list. The data is changing every minute, so I made a service and used a Timer to load the data with an interval.

My question is that:

  • I want to know when the app (not a particular activity) goes to the background, for example, user pressed the home button, in that case, I want to pause the service in order to save battery.
  • I want to know when the phone is sleeping (screen dimmed), in that case, I would like to pause the service too.
  • Any ideas?


  • Pause your service in onPause (and resume in onResume ).
  • Unless you have a wake lock, your app will not run when the phone is sleeping.
  • If you're bound to a foreground app, why use a service in the first place?
  • To elaborate on 1: I understand you're saying "app", not "a particular activity", but your best bet is to use Activity.onPause and Activity.onResume . If anything, you can add a short delay so that nothing happens if onPause is followed immediately by onResume.

    链接地址: http://www.djcxy.com/p/91170.html

    上一篇: 我怎样才能防止进程(脚本)在Android中暂停(冻结)?

    下一篇: 为android应用程序保存数据更新服务