简体   繁体   English

如何在Android中将任何应用程序从后台移到前台(反之亦然)时发送通知

[英]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.

相关问题 将推送通知从Android发送到iOS,反之亦然? - Send push-notification from Android to iOS and vice versa? 我可以将通知从Android发送到iOS,反之亦然吗? - May I send notification from Android to iOS or vice versa? 如何监视我的应用程序是否从前台运行切换到后台运行,反之亦然? - How to monitor if my App switches from running in foreground to running in background and vice versa? 从服务器向Android设备发送消息,反之亦然 - Send message from Server to Android device and vice versa 使用 Twilio 从 arduino 向 Android 应用发送短信,反之亦然 - Send SMS from arduino to Android App Using Twilio and Vice Versa 当接收方设备未在Android中使用FCM处于前台状态时,如何从设备发送推送通知? - How to send Push Notification from device when the receiver device is not in foreground state using FCM in android? 如何将数据从python脚本通过Internet发送到Android应用程序,反之亦然。 - How to send data from python script through Internet to android app and vice versa. Android:从后台启动到前台时保留应用程序状态 - Android:Retain state of application when launch from background to foreground Xamarin Android 从前台通知启动应用程序 - Xamarin Android Launch Application from Foreground Notification 当应用程序在 android 中从后台转到前台时如何重新启动应用程序? - How to relaunch application when app went to background to foreground in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM