How to get last seen in seconds in android?

Hello friends

i am trying to get last seen seconds just like whats up show. here is my code to sent packets

Packet pk = new Packet()
{
@Override
public String toXML()
{
// TODO Auto-generated method stub
return "<iq from='myjid@host,com'   id='last2'   to='friendsjid@host.com'   type='get'> <query xmlns='jabber:iq:last'/></iq>";
}
};
XmppActivity.connection.sendPacket(pk);

My Request is

11-15 15:56:34.805: D/SMACK(26753): 03:56:34 PM SENT (1109640912): <iq from='myjid@host.com'   id='last2'   to='friendjid@host.com'   type='get'> <query xmlns='jabber:iq:last'/></iq>

but if my friend is online, it always returns 0 seconds in the results

11-15 15:54:10.910: D/SMACK(26753): 03:54:10 PM RCV  (1109640912): <iq type="result" id="last2" from="friendjid@host.com" to="myjid@host.com/Smack"><query xmlns="jabber:iq:last" seconds="0"/></iq>

but if my friend is offline, it always returns -1 seconds in the results

11-15 15:54:10.910: D/SMACK(26753): 03:54:10 PM RCV  (1109640912): <iq type="result" id="last2" from="friendjid@host.com" to="myjid@host.com/Smack"><query xmlns="jabber:iq:last" seconds="-1"/></iq>

Please help me to fix this…

Thanks a lot

If you are using asmack/smack you can get last activity that is your last logout time in my case i have done using

LastActivity class from asmack it gives result in seconds and Now you can calculate it

public void getLastSeen(){

try {

LastActivity activity = LastActivity.getLastActivity(XmppActivity.connection, “1234567890@yourdomain”);

Log.d(“LAST ACTIVITY”, activity.lastActivity+"");

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

1 Like

Hello,

I used Smack library and I tried to get the lastactivty like below.

LastActivty lastSeen = LastActivityManager.getLastActivity(ConnectionManager.getInstance().getXmppConn ection(),

Utils.createXmppUserIdByUserId(friendId));

But it gave me the follwing error

08-05 11:59:36.299: E/AndroidRuntime(14006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zobichat.activity/com.MyChat.activity.ChatInitialActivity}: java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$2 cannot be cast to org.jivesoftware.smackx.packet.LastActivity

How can I fix this? Please help.

Please don’t double post: https://community.igniterealtime.org/thread/53205

This method is deprecated. Is there any other way to get last activity?

Which method is deprecated (and in which version)?

public void getLastSeen(){

try {

LastActivity activity = LastActivity.getLastActivity(XmppActivity.connection, “1234567890@yourdomain”);

Log.d(“LAST ACTIVITY”, activity.lastActivity+"");

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

This method " LastActivity.getLastActivity(XmppActivity.connection, “1234567890@yourdomain”); " is deprecated so insted of using this one I used " LastActivityManager.getLastActivity(((Connect) getApplication()).getConnection(), “remoteUsername”); ". But still getting a “No response from server exception”.

I don’t know which Smack version you are referring to. I recommend you have a look at LastActivityManager (smack 4.0.5 API)