Question

In my application I want to simulate Chat room. I have three scenarios of If app is off the screen, If app is on the screen and in Chat activity, If app is on the screen but in other screens.

Server sends notification to my application. GCMIntentService (my receiver) catch it.

  1. If application is off the screen then new notification generates (I have no problem with this and I can generate it) in order to inform user that something happened.

  2. If application is being used by user and he is in Chat screen (for example, user is in Chat screen and he sent a message and is waiting for new message arrives) then update the screen.

  3. If application is being used by user but he is not in Chat screen then blink Chat icon.

I think the first step is to know is application running? I found following code useful, however, it doesn't tell me my app is being used and it's on the screen. Maybe, before app receives message user stopped it by clicking on Home button. Since, the is in memory this code says it's available.

ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++){
        if(procInfos.get(i).processName.equals("MY.PACKAGE.NAME")) {
            Toast.makeText(getApplicationContext(), "TEST MESSAGE", Toast.LENGTH_LONG).show();
        }
    }

Is there any suggestion? I can define a variable in my fragment holder (which is my main Fragment Activity). Is it good way?

Was it helpful?

Solution

My question is answered in this topic: How to know if my android app is visible?

There is a sample here And a sample here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top