How to get last seen using asmack 4.0.5 in android

1.1k Views Asked by At

I am using asmack 4.0.5 and want to retreive last seen of a users. I tried using:

LastActivityManager lActivityManager = LastActivityManager.getInstanceFor(mConnection);

try {
        Log.v(TAG,"Last Activity: " + ActivityManager.getLastActivity(pArg0.getFrom()));

    } catch (NoResponseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XMPPErrorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NotConnectedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But this always returns 0 seconds.It should be like when app is in background then user should be shown as offline.The user is only online when he has app in foreground .Does anybody has any idea how to get last seen of user.

2

There are 2 best solutions below

0
On

One should send a packet with status "Online" when it comes online and "Offline" when it goes offline (when app goes in background, send 'Offline'). Only now server will be able to let other people know when they asks.

Now when A wants to know the last seen status for B and server returns 0, then according to documentation, it means, B is online already.. :) otherwise it will be elapsed time. Read here for more details.

LastActivityManager Source Docs

Official XEP-0012

0
On

As far I worked till now I seen that for getting last seen you have to implement xep 92,and make sure your server support this.else use last activity of your roster.to show last seen.. And maintain DB to store this...and with help of presence packet you can get that.Or modify your message packet to send roster's presence while he/she came online and send other one when he/she goes offline.and put time with this packet.and when you receiver(you) receive this packet then update Db. Hope show you can understand.:)