从后台任务或服务确定当前前台应用程序
我希望有一个在后台运行的应用程序,它知道任何内置应用程序(消息、联系人等)何时运行。
所以我的问题是:
我应该如何在后台运行我的应用程序。
我的后台应用程序如何知道当前在前台运行的应用程序是什么。
如果有经验人士的回复,我们将不胜感激。
I wish to have one application that runs in the background, which knows when any of the built-in applications (messaging, contacts, etc) is running.
So my questions are:
How I should run my application in the background.
How my background application can know what the application currently running in the foreground is.
Responses from folks with experience would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
关于“2.我的后台应用程序如何能够知道当前前台运行的应用程序是什么”。
不要使用
getRunningAppProcesses()
方法,因为根据我的经验,这会返回各种系统垃圾,并且您将获得多个包含RunningAppProcessInfo.IMPORTANCE_FOREGROUND
的结果。使用getRunningTasks()
代替这是我在服务中用于识别当前前台应用程序的代码,它非常简单:
就是这样,然后您可以轻松访问前台应用程序/活动的详细信息:
这需要活动清单中的附加许可并且工作完美。
With regards to "2. How my background application can know what the application currently running in the foreground is."
Do NOT use the
getRunningAppProcesses()
method as this returns all sorts of system rubbish from my experience and you'll get multiple results which haveRunningAppProcessInfo.IMPORTANCE_FOREGROUND
. UsegetRunningTasks()
insteadThis is the code I use in my service to identify the current foreground application, its really easy:
Thats it, then you can easily access details of the foreground app/activity:
This requires an additional permission in activity menifest and works perfectly.
尝试以下代码:
进程名称是在前台运行的应用程序的包名称。将其与应用程序的包名称进行比较。如果相同,那么您的应用程序正在前台运行。
我希望这能回答你的问题。
Try the following code:
Process name is the package name of the app running in foreground. Compare it to the package name of your application. If it is the same then your application is running on foreground.
I hope this answers your question.
我必须通过艰难的方式找出正确的解决方案。下面的代码是cyanogenmod7(平板电脑调整)的一部分,并在android 2.3.3 /姜饼上进行了测试。
方法:
这有望在所有扩展中回答这个问题(:
i had to figure out the right solution the hard way. the below code is part of cyanogenmod7 (the tablet tweaks) and is tested on android 2.3.3 / gingerbread.
methods:
this hopefully answers this issue in all extend (:
从棒棒糖开始,这种情况发生了变化。请先找到下面的代码,然后用户必须进入“设置”->“安全-> (向下滚动到最后一个)具有使用访问权限的应用程序 ->授予我们的应用程序权限
From lollipop onwards this got changed. Please find below code, before that user has to go Settings -> Security -> (Scroll down to last) Apps with usage access -> Give the permissions to our app
为了确定前台应用程序,您可以使用 https://github.com 来检测前台应用程序/ricvalerio/foregroundappchecker。根据设备的 Android 版本,它使用不同的方法。
至于服务,存储库还提供了您所需的代码。本质上,让 android studio 为您创建服务,然后 onCreate 添加使用 appChecker 的代码片段。不过,您需要请求许可。
In order to determine the foreground application, you can use for detecting the foreground app, you can use https://github.com/ricvalerio/foregroundappchecker. It uses different methods depending on the android version of the device.
As for the service, the repo also provides the code you need for it. Essentially, let android studio create the service for you, and then onCreate add the snippet that uses the appChecker. You will need to request permission however.
对于我们需要从我们自己的服务/后台线程检查我们的应用程序是否位于前台的情况。这就是我实现它的方式,它对我来说效果很好:
现在要从其他类检查应用程序是否在前台,只需调用:
Update (如 SHS):
不要忘记在 Application 类的
onCreate
方法中注册回调。For cases when we need to check from our own service/background-thread whether our app is in foreground or not. This is how I implemented it, and it works fine for me:
Now to check from other classes, whether app is in foreground or not, simply call:
Update (as pointed out by SHS):
Do not forget to register for the callbacks in your Application class's
onCreate
method.考虑到
getRunningTasks()
已被弃用且getRunningAppProcesses()
不可靠,我决定结合 StackOverflow 中提到的两种方法:Taking into account that
getRunningTasks()
is deprecated andgetRunningAppProcesses()
is not reliable, I came to decision to combine 2 approaches mentioned in StackOverflow:ActivityManager 类是查看哪些进程正在运行的适当工具。
要在后台运行,您通常需要使用 Service。
The ActivityManager class is the appropriate tool to see which processes are running.
To run in the background, you typically want to use a Service.
这对我有用。但它只给出了主菜单名称。也就是说,如果用户打开了“设置”-->蓝牙 -->设备名称屏幕,RunningAppProcessInfo 将其称为“设置”。无法进一步深入
This worked for me. But it gives only the main menu name. That is if user has opened Settings --> Bluetooth --> Device Name screen, RunningAppProcessInfo calls it as just Settings. Not able to drill down furthur
一个简单的解决方案是使用 LiveData。
创建一个单例 LiveData 变量。 (可能在普通的 Kotlin 文件中)。
从 Activity 或 Fragment 观察:
现在从您的后台服务、广播接收器等:
谢谢。
An easy solution is to use LiveData.
Create a singleton LiveData variable. (probably in a plain Kotlin file).
Observe From Activity or Fragment:
Now from Your Background Service, Broadcast Receiver, etc:
Thanks.
做这样的事情:
Do something like this:
在棒棒糖及以上版本中:
添加到 mainfest:
并执行如下操作:
In lollipop and up:
Add to mainfest:
And do something like this:
这就是我检查我的应用程序是否位于前台的方式。请注意,我按照官方 Android 文档的建议使用 AsyncTask。`
`
并像这样执行 AsyncTask。
新的 CheckIfForeground().execute();
This is how I am checking if my app is in foreground. Note I am using AsyncTask as suggested by official Android documentation.`
`
and execute AsyncTask like this.
new CheckIfForeground().execute();
我在一种方法中结合了两种解决方案,它适用于 API 24 和 API 21。其他我没有测试。
Kotlin 中的代码:
以及 Manifest 中的代码
I combined two solutions in one method and it works for me for API 24 and for API 21. Others I didn't test.
The code in Kotlin:
and in Manifest