英文:
What is a Foreground Service in Android, in essence. LocationListener onLocationChanged
问题
什么是 Android 中的前台服务?我有一个包含活动(Activities)和服务(Services)的应用,清单中使用了 android.permission.FOREGROUND_SERVICE
权限。它需要在活动不在前台时也能获取到 LocationListener
的 onLocationChanged
回调。目前只有在活动在前台时才会收到 onLocationChanged
回调。
它会一直响应发送到其 TCP 监听端口的 TCPIP 请求,即使活动未打开,所以我认为它一直在运行。
我从 Unix/Linux 的角度思考这些事情,即应用/服务/活动都是一个 Linux 进程,清单中的 FOREGROUND_SERVICE
指的是整个进程。我将线程视为基本等效的。
在 Android 中,活动的 ‘主’ 线程是特殊的(用于 UI 控制)。该线程包含一个 Looper
,它是一个消息队列,主 UI 线程会永久地出列消息并处理它们,并调用客户端 UI 回调。
成为一个 ‘前台服务’ 是否与特定线程相关,比如调用了 onCreate
回调的那个线程(我从中调用了 startForeground
方法)?前台服务是否与调用了 onStartCommand
回调的线程相关?我认为整个进程及其所有线程都是 ‘前台服务’。
因此,如果整个事物都不是一个前台服务,那么是什么线程、上下文或部分是前台服务(因此值得接收到 onLocationChange
回调),即使活动未显示?
(注意:我曾对前台和后台这些术语感到困惑。我认为 ‘服务’ 是一种提供服务但没有可见 UI 的 ‘后台’ 事物。在 Android 中,‘前台服务’ 这个术语意味着一个服务在用户没有直接交互时也会执行用户可感知的任务。因此,我认为 ‘前台服务’ 有点误导性。)
英文:
What really is a foreground service in Android? I have an app with Activities, and Services and uses-permission android.permission.FOREGROUND_SERVICE in the manifest. It needs to get LocationListener onLocationChanged callbacks even when the Activity in not in the foreground. Currently it only gets onLocationChanged callbacks when the Activity is in the foreground.
It does respond to TCPIP requests sent its TCP listen port all the time, even when the Activity is not open, so I think it is alive all the time.
I think of these things from a Unix/Linux sense, that the App/Service/Activities are all one Linux process and that the FOREGROUND_SERVICE in the manifest refers to the whole process. I think of threads as all being more of less equivalent.
In Android, the 'main' thread of an activity is special (for the UI control). That thread contains a 'Looper? Which is a message queue and presumably the main UI thread perpetually dequeues messages and processes them and calls client UI callbacks.
Is being a 'foreground service' something associated with a particular thread, like the one that calls the 'onCreate' callback for the 'Service' (from which I call the startForeground method?) Is the foreground service associated with thread that calls-back the onStartCommand callback? I would think that the whole process and all its threads are the 'foreground service'.
So, if the whole thing is not a foreground service, what thread or context or part of the thing is a foreground service (and thus worthy of getting onLocationChange callbacks even if the Activity is not being shown)?
(Note: I've been confused by the terms foreground and background. I thought that 'services' were sort of 'background' things, that provide services, but have no visible UI. The term 'foreground service' in Android means a service that: performs a task that is noticeable by the user, even when they're not directly interacting with the user. So I think 'foreground service' is a bit of a misnomer.)
答案1
得分: 2
你是否拥有后台定位权限?在现代Android中,后台获取位置需要特殊权限。请查看https://developer.android.com/about/versions/oreo/background-location-limits和ACCESS_BACKGROUND_LOCATION权限。
但是,回答你的问题-在Android中,Service只是一个与UI无关的Context实例。它没有与之关联的特定线程(所有生命周期方法都在主线程上运行,就像所有生命周期方法一样)。它是主进程的一部分。没有什么特别的,它只是一个具有自己生命周期的Android Context。
前台服务是由操作系统标记为前台的Android服务。这不是一个公开可设置的字段,而是由操作系统存储在内存中的信息。前台服务具有额外的特权(后台服务不允许执行某些操作,如录制音频),并且不太可能因资源问题而被系统终止。但它的结构或线程没有任何改变。
英文:
Do you have background location permission? On modern Android, getting location in the background requires special permission. See https://developer.android.com/about/versions/oreo/background-location-limits and ACCESS_BACKGROUND_LOCATION permission
But to answer your question- a Service in Android is just an instance of Context that is not associated with a UI. It has no particular thread associated with it (the lifecycle methods all run on the main thread, like all lifecycle methods do). It's part of the main process. There's nothing special about it, it's just an Android Context with it's own lifecycle.
A Foreground Service is an Android Service that's marked as being in the foreground by the OS. This is not a publicly settable field, it's something stored by the OS in its memory. Foreground services have additional privlidges (there are things that background services aren't allowed to do, like record audio) and are less likely to be ended by the system for resources. But nothing changes about it's structure or threads.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论