[英]How to send notification when any application moves from background to foreground and vice versa in Android
Is there any way to know when any application comes from background to foreground and vice-verse ? 有什么办法知道什么应用程序何时从后台运行到前台,反之亦然? I need some kind of mechanism for my own app to know that any application installed in device went from background to foreground (Main Screen) or went to background to foreground .
我需要某种机制使自己的应用程序知道设备中安装的任何应用程序都是从后台运行到前台(主屏幕),还是从后台运行到前台。
I do not think this is possible in Android for security reasons. 出于安全原因,我认为这在Android中是不可能的。 If you want to see when applications are launched you must create a custom launcher.
如果要查看何时启动应用程序,则必须创建一个自定义启动器。
Even so, I have been looking to do this for sometime and the closest I could get to similar functionality was to monitor running apps like so: 即便如此,我一直在寻找这样做的时间,而与类似功能最接近的是监视运行的应用程序,如下所示:
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningTasks = activityManager.getRunningAppProcesses();
So, if 因此,如果
runningTasks.get(0).importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
then that task is in the foreground (if not then it is in background). 则该任务位于前台(如果没有,则位于后台)。
For checking if my app is in foreground, I was using this code: 为了检查我的应用程序是否处于前台,我使用了以下代码:
public static boolean isAppInForeground(Context context) {
List<RunningAppProcessInfo> appProcesses = ((ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.