Reputation: 1219
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 ??
Upvotes: 3
Views: 8219
Reputation: 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
Upvotes: -1
Reputation: 1073
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();
}
Upvotes: 3
Reputation: 1431
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
Upvotes: 2
Reputation: 9033
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();
}
Upvotes: 1