如何从服务中检查应用程序是否在前台?

How to check is app in foreground from service?

仅当应用程序不在前台时,我才需要向用户显示通知。这是我的 public class MyFirebaseMessagingService extends

FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if(applicationInForeground()) {
            Map<String, String> data = remoteMessage.getData();
            sendNotification(data.get("title"), data.get("detail"));
        }

    }

需要实现applicationInForeground()方法

您可以从 android 系统服务控制 运行 应用进程。试试这个:

private boolean applicationInForeground() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();
    boolean isActivityFound = false;

    if (services.get(0).processName
            .equalsIgnoreCase(getPackageName()) && services.get(0).importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
        isActivityFound = true;
    }

    return isActivityFound;
}

祝你好运。

在 Google I/O 2016 年,我做了一个演讲,其中一个主题是如何 Firebase detects if your app is in the foreground. You can use ActivityLifecycleCallbacks for that by incrementing a counter for every activity in your app that gets started, then decrementing it for each activity that gets stopped. If the counter is > 1, then your app is in the foreground. The relevant part of the talk can be seen on YouTube here

您也可以尝试使用 Android Jetpack lifecycle components

public class AppFirebaseMessagingService extends FirebaseMessagingService implements LifecycleObserver {

    private boolean isAppInForeground;

    @Override
    public void onCreate() {
        super.onCreate();

        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onForegroundStart() {
        isAppInForeground = true;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onForegroundStop() {
        isAppInForeground = false;
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if(isAppInForeground) {
            // do foreground stuff on your activities
        } else {
            // send a notification
        }
    }
}

请参阅 here 如何导入必要的依赖项,因为生命周期不是标准 Android SDK 的一部分。

我刚刚报告了 @bompf

的 Kotlin 版本
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class AppFirebaseMessagingService : FirebaseMessagingService(), LifecycleObserver {

    private var isAppInForeground = false

    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onForegroundStart() {
        isAppInForeground = true
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onForegroundStop() {
        isAppInForeground = false
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (isAppInForeground) {
            // do foreground stuff on your activities
        } else {
            // send a notification
        }
    }
}