我是机器人的菜鸟。我希望在web仪表板上显示设备状态,无论设备是否脱机/联机。GCM对此是否有帮助,或者我应该为此使用什么。
或者我应该打电话给任何一个Web,将手机状态从手机发送到服务器,直到手机打开,这样我就可以在网络仪表板上显示手机的“在线”状态??
设备关闭时的脱机状态&设备打开时的联机状态
知道吗??
发布于 2014-04-15 13:00:03
使用这种方法:您不应该发送ping到server.Server,应该在某个时间间隔后发送ping到设备,设备应该回复。如果设备没有应答,这意味着用户离线。基本上,您需要创建与服务器的套接字连接并交换ping。
在openfire服务器上也实现了同样的功能。
发布于 2014-04-15 13:09:04
步骤1广播接收器的->注册以检查离线/联机设备状态。
在广播接收器的onReceive()方法中,检查网络更改,如果有更改,请转到步骤2。
步骤2 ->获取设备状态并与POST参数"device_status“一起调用web。
使用下面的API获取Internet连接的状态。
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();
}
发布于 2014-04-15 13:05:26
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;
}
把它叫做
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();
}
https://stackoverflow.com/questions/23084389
复制相似问题