始终运行Android应用程序 [英] Always running Android application

查看:270
本文介绍了始终运行Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我还不了解一些Android的概念,但是回答下列问题可以帮助我很多:

在我的Andr​​oid应用程序,我需要从Web服务获取数据在后台每个x秒(应用程序必须似乎没有运行)。

在从Web服务中的数据是一个我期待,我也需要显示的Andr​​oid通知。


  • 那么如何才能载入我的应用程序在一个具体的活动?

  • 如何确保后台服务将继续运行,即使它崩溃?这是,我如何重新启动我的应用程序的背景部分或服务?


解决方案

  

在我的Andr​​oid应用程序,我需要从一个Web服务每个x获取数据
  在后台秒(应用程序必须似乎没有运行)。


  
  

我可以使用Android服务?
   http://developer.android.com/reference/android/app/Service.html


是的,你可以使用一个服务,但问题是你要轮询每x秒Web服务?您将电池电量的消耗速度非常快的方式。
你可以做的要么是安排一个不精确的重复任务(这将大约15分钟的间隔调用,但会与其他应用程序被揉成降低电池的使用)。见AlarmManager.setInexactRepeating()。
或使用谷歌云消息传递(GCM),让您的服​​务器通知,醒来你的电话的时候有新的数据使用推送通知。


  

在从Web服务中的数据是一个我期待,我做的
  需要显示的Andr​​oid通知。那么如何才能载入我的应用
  在具体的活动?


您可以从服务一举两得。 A服务是一种背景下,这样你就可以显示通知,启动和活动等...


  

我如何能确保后台服务将继续运行,即使它
  崩溃?这是,我该如何重新启动背景部分或服务
  我的应用程序?


您可以安排一个重复的服务开始在AlarmManager意图。如果你的服务正在运行,它应该被忽略,如果没有 - 它会启动您的服务

要确保你的服务不被系统终止容易(在低内存为例),使用Service.startForeground(),并确保从Service.onStartCommand()返回START_STICKY。

Maybe I don't understand yet some of the Android concepts, but answering the following questions may help me a lot:

In my Android app, I need to get data from a web service each x seconds in background (application must seem to not be running).

When the data from the web service is the one I am expecting, I do need to show an Android Notification.

  • How can then load my Application in a concrete activity?
  • How can I ensure the background "service" will keep running even if it crashes? This is, how do I restart that background part or "service" of my application?

解决方案

In my Android app, I need to get data from a web service each x seconds in background (application must seem to not be running).

May I use Android Services? http://developer.android.com/reference/android/app/Service.html

Yes, you may use a Service but the question is do you want to poll a web service every x seconds? You will drain the battery VERY fast that way. What you could do is either schedule an inexact repeating task (it will be called APPROXIMATELY in 15 minutes intervals but will be bunched with other apps to lower battery usage). See AlarmManager.setInexactRepeating(). Or use push notifications using Google Cloud Messaging (GCM) to let your server notify and wake up your phone when there is new data.

When the data from the web service is the one I am expecting, I do need to show an Android Notification. How can then load my Application in a concrete activity?

You can do both from the Service. A Service is a Context, so you can show Notifications, start Activities and such...

How can I ensure the background "service" will keep running even if it crashes? This is, how do I restart that background part or "service" of my application?

You can schedule a repeating Service start Intent in the AlarmManager. If your Service is running, it should be ignored, if not - it will start your Service.

To ensure your Service is not killed by the system easily (when low on memory for example), use Service.startForeground() and be sure to return START_STICKY from Service.onStartCommand().

这篇关于始终运行Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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