Android的服务和活动 [英] Android Services and Activities

查看:97
本文介绍了Android的服务和活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android的服务和活动的生命周期的一些问题。希望你们能帮助我。我写了使用GPS定位和存储他们的应用程序。它必须不断地监视GPS位置更新才能正常工作。

I have some questions about the Android service and activity lifecycles. Hope you guys can help me out. I have written an application that uses GPS location and stores them. It must monitor the GPS location updates constantly to work properly.

现在,这一切都在一个活动。所以,当您关闭活动,你失去一切,不能继续监视位置更新。这就是为什么我要拍的监控部分后台服务。我一直在阅读的Andr​​oid开发者指南和参考,但仍不能完全理解活动的业务流程生命周期的事情。

Right now, it's all in one activity. So when you close the activity, you loose everything and can't continue monitor location updates. This is why I wanted to make a background service for the monitoring part. And I've been reading the Android Developer Guide and the Reference, but still can't fully understand the activity-service-process-lifecycle thing.

考虑一下:你启动应用程序。它得到一个GPS定位,现在你可以开始监视位置更新和保存。当你这样做的话,它应该开始接收GPS位置更新的服务。所以,现在你有一个活动,并在同一个进程中运行的服务。好。但是,如果你把你的手机上的硬件后退按钮会发生什么?只能将活动与过程仍在运行的服务杀?还是会杀了两个,活动和服务,也因此而这个过程?有一些方法,以保证服务保持在同一个进程中运行的活动被杀害后?但这样并不复杂的整体AIDL事情远程进程?

Consider this: you start the application. It gets a GPS fix and now you can start monitoring the location updates and save them. When you do so, it should start a service that receives GPS location updates. So now you have an activity AND a service running in the same process. Ok. But what happens if you push the hardware Back-button on your phone? Will only the activity be killed with the process still running your service? Or will it kill both, the activity and the service and thus also the process? Is there some way to ensure the service keeps running in the same process after the activity is killed? But a way not so complicated as the whole aidl thing for remote processes?

在一个新的进程创建服务似乎没有必要对我来说,因为没有其他应用程序应该使用我的服务,我只需要监控的一部分,以保持运行时(GUI)的活动已经死了。我想让类似的开发者网站上的本地服务的例子,但我必须确保服务继续运行该活动被杀害后。

Creating a service in a new process seems unnecessary to me, because no other application should use my service, I just need the monitoring part to keep running while the (GUI) activity is dead. I wanted to make something like the Local Service example on the Developer site, but I must be sure the service keeps running after the activity is killed.

推荐答案

当用户点击返回按钮,它是不同的,当系统结束处理应用程序在运行的电流活动完成()但这个过程仍然活着。 Android将,只要存储器允许它,保持服务活着不管活性是否是可见的。

When the user hits the back button it is different that when the system ends the process that the application was running in. The current activity will finish() but the process remains alive. Android will, as long as memory permits it to, keep the service alive regardless of whether or not the activity is visible.

从文档

如果该服务已启动,   那么它的宿主进程被认为是   比任何不太重要   当前可见的过程   到屏幕上的用户,但更   比任何过程并不重要   可见。因为只有几道工序   一般对用户可见,   这意味着该服务不应   除非在极端低内存被杀死   条件。

If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible. Because only a few processes are generally visible to the user, this means that the service should not be killed except in extreme low memory conditions.

服务不应但留在记忆中永远,这是不好的做法,不正是你想要的。我会建议使用 IntentService 加上通过 LocationListener的 LocationManager 到文档的链接

Services should NOT however remain in memory forever, this is bad practice and not exactly what you want. I would recommend using an IntentService coupled to receive location updates via a LocationListener and LocationManager link to docs

public void requestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent)

此设置应该为你服务得很好,因为每次更新,你可以火了一个意图可以通过你的服务来处理,如果不是活的,将被创建。

This setup should serve you quite well because on each update you can fire off an Intent which can be handled by your Service, if it is not alive it will be created.

这篇关于Android的服务和活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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