2

I am designing an app which needs to know which users are online or offline. Each user can notify the server if he is online or offline and then I can get that information from the server.

The issue is that is there a standard in android which marks the user as online or offline.

One solution is if the app is running then the user is online, if the app force stopped or not running then the user is offline. So in my app how will I know that the app is stopping?

If the above is not possible or not a good solution then when will my app tell the server that it is going offline.

Thanks in advance.

Sohan Alam
  • 529
  • 1
  • 6
  • 12

2 Answers2

0

In Android, there is no hard solid concept of "leaving an application". Apps can be pushed on the stack by pressing home, still alive in the background or pushed away from top by another application. There is no real hook that you can use to say "ok, I am really going down now, I should notify the server".

So, you could take it the other way around : the server grants a bail to a user, for 5mn let's say, and the client app has to renew it. After 5 mn of inactivity, the server can know that a user is going offline.

Off course, the delay will be a trade off between the need of precision/responsiveness you want on the server side and battery usage as this will tend to decrease battery life by constantly notifying the server that the client is still online.

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Thanks for the response. I am thinking this approach can also put a lot of stress on the server if there are like a million users running the app. Not sure I am not too familiar with server implementations? – Sohan Alam Nov 14 '13 at 05:55
0

In Android When application is create onCreate() method of Application class is calls when application is force closed onTerminate() method is calls.

If you want to do something when application is create and terminate you need to create your custom application class by extending Application class.

1) Create your own application class like this

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
                // When application creates

    }

    @Override
    public void onTerminate() {
        super.onTerminate();
                // calls when application force closed.

    }

}

2) In your manifest.xml

<application
        android:name="com.yourpackagename.MyApplication"
        android:allowTaskReparenting="true"
        android:debuggable="true"
        android:icon="@drawable/ijoomer_luncher_icon"
        android:label="@string/app_name"
        android:theme="@style/ijoomer_theme" >
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Can I be sure that OnTerminate() will be called, even when the phone is powered off. The previous answers suggests that there is no way to tell the server that the application is going down!! – Sohan Alam Nov 14 '13 at 06:03
  • onTerminate() will calls when application is force close as you want. But not call when application in background. – Biraj Zalavadia Nov 14 '13 at 06:06