0

I'm building an App which does some communications with a server using GCM. It works all fine but one. I've implemented an instant chat which sends notifications to the other android device that there is a new message. If the other device clicks on the noti, it will enter the instant chat room to reply, or just see newly updated messages.

I'm doing this by comparing a flag from the server ; if the message is the first one to send and the app or device is sleeping, it will make a noti which can link to the new chat room. If the message is from the currently running chat room, it will just update it.

But as you can easily see, it can't do anything if the device receiving the message is not awake.

So I want to tell if the device state is awake of not to decide whether the message is from a new chat room, or from a current one. If it's from a new one(when the device is sleeping), it will display a noti which will lead to the newly made chat room, and if it's from the current chat room, it will just update the chat log.

March3April4
  • 2,131
  • 1
  • 18
  • 37
  • what seems to be the problem ? you can have a broadcast receiver for device screen off and on and update a field which you can check – Abhishek Nandi Nov 18 '14 at 18:53
  • If your code is executing, the device is awake (though the screen may be off). As far as your server is concerned, if the device is exchanging traffic, it is awake. If it is not (after some timeout) then it is either asleep or (temporarily?) without network - which may not be all that different for application purposes. – Chris Stratton Nov 18 '14 at 19:16

2 Answers2

0

I think you need to study PowerManager API for android. PowerManager Link

From what i can tell you can use the method isInteractive() or isScreenOn() depending on what version of android your on(isScreenOn was deprecated in API level 20 and is replaced by isInteractive) to check if the screen is off.

But i will advise against using isScreenOn() there is no guarantee that the device will go to sleep after the screen is turned off. This is upto the OEM.

Best you can do is keep the device awake using Wakelock Wakelock Link

0

I solved this issue by following this link. How can I tell if Android app is running in the foreground?

by the answer of Gadenkan, I made an activityManager object to check if my app is running in the foreground.

So, if it is not running on the foreground, it will call the entire app to start from the main activity to the chat room. If not, it will just tell the broadcastlistener to append new messages by the GCM.

Community
  • 1
  • 1
March3April4
  • 2,131
  • 1
  • 18
  • 37