如何知道我的Andr​​oid应用程序是可见的?我的、应用程序、Andr、oid

2023-09-04 10:18:50 作者:▼丶情绪控Ⅰ╰

我probleme是,我有一个计时器,它结束时启动的通知。不过,我想用notificationManager只有当应用程序是当前不可见火的通知,并显示一个alertDialog如果定时器终止,而应用程序是在前台。

我已经尝试过这一点:

  ActivityManager actMngr =(ActivityManager)ValeoMobileApplication.getContext()getSystemService(Activity.ACTIVITY_SERVICE)。
    名单< RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
    Tools.log(TimerBroadcastReceiver,的onReceive,所有正在运行的工艺如下:);
    对于(RunningAppProcessInfo PI:runningAppProcesses){
        //检查pi.processName,做你的东西
        //同时检查丕重要性 - 检查过程是在前台或后台
        Tools.log(TimerBroadcastReceiver,的onReceive,pi.processName +重要性=+ pi.importance);
        如果(pi.processName.equalsIgnoreCase(MY_APP_PROCESS_NAME)){
            如果(pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                isApplicationInForeground = TRUE;
            }
        }
    }
 

但它似乎并不重要,如果应用程序是前景与否,其重要性永远不会改变......可能有人帮助?

谢谢!

解决方案   

不过,我想用notificationManager只有当应用程序是当前不可见火的通知,并显示一个alertDialog如果定时器终止,而应用程序是在前台。

使用一个有序的广播,与具有高优先级的接收器,如果它是在前景清单注册接收机的情况下的活性,加上当它不是在前台。

这里是一个博客帖子概述了这种技术。 这里是证明这种技术的示例项目。

My probleme is that I have a timer, that start a notification when it ends. But I would like to fire a notification using notificationManager only if the app is not currently visible, and to show an alertDialog if the timer ends while the app is in foreground.

I've already tried with this :

    ActivityManager actMngr = (ActivityManager) ValeoMobileApplication.getContext().getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
    Tools.log("TimerBroadcastReceiver", "onReceive", "All running processes are listed below :");
    for (RunningAppProcessInfo pi : runningAppProcesses) {
        //Check pi.processName and do your stuff
        //also check pi importance - check if process is in foreground or background
        Tools.log("TimerBroadcastReceiver", "onReceive", pi.processName + " importance = "+pi.importance);
        if(pi.processName.equalsIgnoreCase("MY_APP_PROCESS_NAME")){
            if (pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                isApplicationInForeground = true;
            }
        }
    }

But it seems that it doesn't matter if the app is foreground or not, its "importance" never changes... Could someone helps?

Thanks !

解决方案

But I would like to fire a notification using notificationManager only if the app is not currently visible, and to show an alertDialog if the timer ends while the app is in foreground.

Use an ordered broadcast, with the activity having a high-priority receiver if it is in the foreground, plus a manifest-registered receiver for the cases when it is not in the foreground.

Here is a blog post outlining this technique. Here is a sample project demonstrating this technique.