我如何知道 Android 应用程序的应用程序状态以显示或不显示通知?

How can I know the application state of an Android App to display or not a notification?

我正在开发一个使用 Google 云消息 的即时消息应用程序。当用户收到来自联系人的消息时,我会发送 通知 。一切正常。我设置了 GCMBroadcastReceiverGCMMessageHandler。问题是:通知 总是 显示,即使用户当前正在与他的联系人聊天。 如何知道 GCMMessageHandler 中的应用程序状态而不显示通知?

这是 GCMBroadcastReceiver :

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // Explicitly specify that GcmMessageHandler will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmMessageHandler.class.getName());

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

这是 GCMMessageHandler:

protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    mes = extras.getString("message");

    if(IM_NOT_IN_THE_CHAT_ACTIVITY)
    {
        showNotification(mes);
    }

我努力得到的是那个标志:IM_NOT_IN_THE_CHAT_ACTIVITY.

我如何在 IntentService 中知道这一点?

我猜这样做会成功:

    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
    String activity_name = taskInfo.get(0).topActivity.getClassName();


    if(activity_name != ACTIVITY_NAME)
        showNotification(mes);