簡體   English   中英

我如何知道要顯示或不顯示通知的Android應用程序的應用程序狀態?

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

我正在開發一個使用Google Cloud Messaging的即時消息應用程序。 當用戶收到聯系人的消息時,我會發送通知 一切正常。 我設置了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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM