3

I am noob in Android. I want to display device status at web dashboard whether device is offline/online. Is GCM helpful for this to do or something what should I use for this .

Or Should I call any Web API continuous to send the phone status from phone to server until phone is ON so that I can display here "Online" status of phone at web dashboard ??

Offline status when your device is OFF & Online status when your device is ON

Any idea ??

Mick
  • 1,179
  • 5
  • 14
  • 24
  • 3
    GCM will work here, as soon as the phone receive the GCM message make a request back to server that I am online and receiving messages otherwise offline – Syed Waqas Apr 15 '14 at 13:12

4 Answers4

3

Step 1 -> Register for the Broadcast Receiver to check for device status offline/online.

Inside the onReceive() method of the Broadcast Receiver, check for network changes, if there is a change go to step 2.

Step 2 -> Get the device status and call the web api along with the POST parameter "device_status".

Use the below API to get status for Internet Connectivity.

public boolean testNetwork(Context context) {

    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if ((connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null && connManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected())
            || (connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null &&  
            connManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                    .isConnected())) {
        return true;
    } else {
        return false;
    }
}

BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean connectivity = CommonUtils.getInstance().testNetwork(
                BaseActivity.this);
        if (!connectivity) {
            // write your code
        } else {
            //write your code
        }

    }
};

IntentFilter filter = new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION);
    try {
        registerReceiver(networkStateReceiver, filter);
    } catch (Exception e) {
        e.printStackTrace();
    }
Sunny Garg
  • 1,073
  • 1
  • 6
  • 13
  • It is unrelated to my question. You should update your answer – Mick Apr 21 '14 at 20:19
  • Here you're describing how to check network status locally, on the android device. But the complexity of the initial question comes from the "web dashboard status" : how the server can be aware of client availability ? – Xavier D Sep 26 '19 at 07:20
2

use this method : You should not send ping to server.Server should send ping after some time interval to the device and device should reply. If device is not replying this means user is offline. Basically you need to create socket connection with server and exchange ping

same thing is implemented on openfire server

Jaspreet Chhabra
  • 1,431
  • 15
  • 23
  • How to impliment this? Say I have 1000 users, Do I need to send "heart beat" to all of them?" and When to do that? Every minute?? , the other chat apps uodate the status IMMEDIATLY!! – Esmaeel Ibraheem Aug 15 '15 at 16:18
1
public boolean isOnline() {
        ConnectivityManager conMgr = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

        if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
            /*
             * Toast.makeText(getActivity(), "No Internet connection!",
             * Toast.LENGTH_LONG).show();
             */
            return false;
        }
        return true;
    }

and call it like

if (isOnline()) {
//code if online
} else {
            AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                    .create();

            alertDialog.setTitle("Info");
            alertDialog
                    .setMessage("Internet Not Available, Cross Check Your Internet Connectivity and Try Again!");
            alertDialog.show();
        }
Android
  • 8,995
  • 9
  • 67
  • 108
-1

Implement XMPP Protocol along with any Jabber server. It gives you correct info because it was created to make the offline/ online system

for help use link below

https://xmpp.org/about/