我有一项跟踪用户位置的服务。目前,服务在应用程序启动时启动,在应用程序终止时停止。不幸的是,如果用户将应用程序保留在后台,该服务将永远不会停止并耗尽电池。
我希望当我的应用程序不在前台时停止服务。我希望 Application 类能让我覆盖 onPause 和 onResume 处理程序,但它没有它们。我还有其他方法可以完成这个任务吗?
I have a Service which tracks the location of the user. Currently, the Service boots when the application starts and stops when the application terminates. Unfortunately, if users keep the application in the background, the Service never stops and drains battery.
I would like the Service to stop when my application is not in the foreground. I was hoping the Application class would let me Override onPause and onResume handlers, but it does not have them. Is there another way I can accomplish this?
发布评论
评论(8)
我还没有对此进行测试,但看起来如果您使用 Context#bindService() (而不是 Context#startService() ),则服务应该在没有任何情况下停止更多的活动必然与之相关。 (请参阅服务生命周期)。
然后在每个 Activity 中使用
onPause()
/onResume()
来绑定/取消绑定服务。或者,您可以在服务上添加一对方法,告诉它开始/停止侦听位置更新,并从每个活动的
onResume()
/onPause()
调用它。该服务仍将运行,但位置更新不会耗尽电池电量。I haven't tested this yet, but it looks like if you use
Context#bindService()
(instead ofContext#startService()
), the service should stop when no more activities are bound to it. (see Service lifecycle).Then use
onPause()
/onResume()
in each activity to bind/unbind from the service.Alternatively, you could add a pair of methods on your service which tell it to start/stop listening for location updates and call it from each activity's
onResume()
/onPause()
. The service would still be running, but the location updates wouldn't be draining the battery.阅读以上所有答案,我建议只需为每个活动添加一个布尔全局标志即可把它放在你的 onResume & 中onPause & 暂停也在启动一个 Activity 时类似这样
,onResume 和 onResume 也一样
。类似地,当启动一个新的活动时
,然后在您的服务中只需检查
并;你完成了!
Reading all the above answers I would suggest Simply add a boolean global flag for each activity & put it in your onResume & onPause & also while launching an Activity Something like this
&same for onResume
& similarly when launching a new Activity
then in your Service simply check
& you are done!
您应该覆盖 onPause 和 onResume 方法/developer.android.com/reference/android/app/Activity.html" rel="nofollow">活动。如果您有多个活动,您可能希望为它们拥有一个公共基类,并将开始/停止逻辑放入基类中。
You should override the onPause and onResume methods on your Activity. If you have multiple activities you may want to have a common base class for them and put the start/stop logic into the base class.
我还没有尝试过这种方法,但我认为您可以使用
KeyEvent.KEYCODE_HOME
覆盖 Android 设备的 home 键,并且可以使用stopService(Intent)
来停止您的服务并当应用程序再次恢复时,您可以在 Activity 的 onResume() 方法中编写startService(Intent)
。这样,我认为只有当用户明确按下主页按钮在后台获取应用程序时,您的服务才会停止,而不是当他从一个活动切换到另一个活动时。
I have not tried this approach but I think you can override the home key of android device by using
KeyEvent.KEYCODE_HOME
and you can usestopService(Intent)
to stop your service and when again application resumes, you can writestartService(Intent)
in the onResume() method of your Activity.This way I think your service will only stop when user explicitly presses home button to take application in the background and not when he switches from one activity to another.
我建议像其他人所说的那样重写 onPause/onReume 方法。如果不了解有关应用程序流程以及活动之间交互的更多信息,我无法提供超出猜测的更多信息。
但是,如果您的活动是持久的,我的建议是在活动之间切换时更好地利用意图。
例如,每个活动都应该有一个布尔“转换”标志。因此,当您从一个 Activity 移动到下一个 Activity 时,您可以额外设置一个 Intent:
在接收 Activity 中遵循:(在 onCreate 中)
这样,对于启动的每个 Activity,您可以知道它是否来自另一个 Activity,或者如果它是从主屏幕启动器启动的。因此,如果它获得真正的转换,那么 onPause 不应该停止服务——这意味着您将在它返回后返回到上一个 Activity。如果它没有收到额外的“转换”,或者错误的转换,那么您可以放心地假设它下面没有 Activity 等待接管当前的 Activity。
在第一个活动上,如果要切换到另一个活动,您只需要停止服务,您应该能够以编程方式确定一个活动是否是从另一个活动启动的。
What I would suggest is overriding the onPause/onReume methods as others have said. Without knowing more about the flow of your application and interactions between Activities, I can't give much more information beyond guesswork.
If your Activities are persistent, however, my recommendation would be to utilize the Intents better when switching between Activities.
For instance, each Activity should have a boolean "transition" flag. So, when you move from one Activity to the next, you set up an Intent extra:
Followed in the receiving Activity by: (in onCreate)
This way, for each Activity that launches, you can know whether it has come from another Activity, or if it has been launched from a home screen launcher. Thus, if it gets a true transition, then onPause should NOT stop the service--that means you will be returning to the previous Activity after it returns. If it receives no "transition" extra, or a false transition, then you can safely assume there is no Activity underneath it waiting to take over for the current one.
On the first Activity, you will simply need to stop the service if you are switching to another Activity, which you should be able to figure out programmatically if one Activity is started from another.
听起来真正的问题是如何仅在您参加不属于您自己的活动时停止服务?一种方法是在 onPause 方法中停止活动。对您的所有活动都这样做。然后重写你的 startActivity 方法。在这里进行一项条件测试,以确认您是否有目的地导航到您自己的测试。如果您将标志设置为 true。
现在返回到暂停时重写的方法。并且仅当标志不等于 true 时才停止服务。将标志设置为 false。
所有离开的事件都将关闭您的服务。导航到您自己的位置将保持其完好无损。
在所有活动扩展的基类中进行重写。
用我的 android 写的。稍后将发布 ezaple。
It sounds like the real problem is how to only stop the service when you go to an activity that isn't one of your own? One way would be to in your onPause method to stop the activity. Do this for all your activities. Then override your startActivity method. And in here do a conditional test to confirm that you are purposefully navigating to one of your own. If your are set a flag to true.
Now go back to your on pause overridden method. And only stop your service if the flag is not equal to true. Set the flag to false.
All events that navigate away will close your service. Navigating to your own will leave it intact.
Do the overriding in a base class that all your activities extend.
Writeen in my andolroid. Will post ezaple later.
尝试使用绑定服务技术来完成此任务。
绑定服务 | Android 开发人员
您可以以某种方式使用绑定服务,以便当没有任何活动绑定到该服务时该服务将停止。这样,当应用程序不在前台时,服务将不会运行。当用户将应用程序带回到前台时,活动将绑定到服务,并且服务将恢复。
Try using the Bound Services technique to accomplish this.
Bound Services | Android Developers
You can use bound services in a way such that the service will stop when no activities are bound to it. This way, when the app is not in the foreground, the service will not be running. When the user brings the app back to the foreground, the Activity will bind to the service and the service will resume.
在您的 Application 对象中创建
registerActivity()
和unRegisterActivity()
方法,并在所有操作onResume()
中实现第一个方法,在 actionsonResume()
中实现第二个方法 <代码>onPause()。第一个方法将活动添加到应用对象中的
List
实例,unRegisterActivity()
在每次调用中检查列表的大小if==0 stopService();
。Create methods
registerActivity()
andunRegisterActivity()
in your Application object and implement first method in all you actsonResume()
and second in actsonPause()
.First method add activity to
List<Activity>
instance in your app object,unRegisterActivity()
checks size of list in every callif==0 stopService();
.