Android, How can I check that my application is visible to the user (Chat implementation)?

In my application, I want to simulate a chat. I have three If scripts when the application is disconnected from the screen. If the application is on the screen and in chat activity, if the application is on the screen, but on other screens.

The server sends a notification to my application. GCMIntentService (my receiver) will catch it.

  • If the application is disconnected from the screen, a new notification is generated (I have no problems with this, and I can generate it) to inform the user that something has happened.

  • If the application is used by the user and he is in the chat window (for example, the user is on the chat screen and he sent a message and is waiting for a new message to arrive), then refresh the screen.

  • If the application is used by the user, but it does not appear on the chat screen, then click the chat icon.

I think the first step is to know if the application is working? I found the following code useful, however, it does not tell me that my application is being used and it is on screen. Perhaps, before the application receives the message, the user stopped it by clicking the "Home" button. Since in memory this code says that it is 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 function). Is this a good way?

+4
source share
1 answer

My question is answered in this section: How do I know if an Android application is visible?

Here is an example here. And a sample here.

+1
source

Source: https://habr.com/ru/post/1483542/


All Articles