我有一個計時器,它在結束時開始通知。但是我想僅在應用程序當前不可見的情況下使用notificationManager觸發通知,並且在應用程序處於前臺時計時器結束時顯示alertDialog。如何知道我的android應用是否可見?
我已經嘗試過這一點:
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;
}
}
}
但似乎如果應用程序是前景或沒有也沒關係。我怎樣才能做到這一點?
謝謝,這就是我一直在尋找的! – PhilippeAuriach
如果您的主要活動正在啓動另一項新活動,則這不起作用。那時,由於onPause被觸發,取消註冊將會發生。但是,從最終用戶的角度來看,該應用程序仍然可見。 –
據我所知,這種方法不再適用於Android Oreo。因爲不可能通過manifest來註冊隱式接收器。 – MyDogTom