android: Add method to check for connectivity to NetworkManager
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2013 Tobias Brunner
|
||||
* Copyright (C) 2012-2015 Tobias Brunner
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -90,8 +90,8 @@ METHOD(network_manager_t, add_connectivity_cb, void,
|
||||
this->connectivity_cb.cb = cb;
|
||||
this->connectivity_cb.data = data;
|
||||
}
|
||||
androidjni_detach_thread();
|
||||
}
|
||||
androidjni_detach_thread();
|
||||
}
|
||||
this->mutex->unlock(this->mutex);
|
||||
}
|
||||
@@ -132,6 +132,28 @@ METHOD(network_manager_t, remove_connectivity_cb, void,
|
||||
this->mutex->unlock(this->mutex);
|
||||
}
|
||||
|
||||
METHOD(network_manager_t, is_connected, bool,
|
||||
private_network_manager_t *this)
|
||||
{
|
||||
JNIEnv *env;
|
||||
jmethodID method_id;
|
||||
bool connected = FALSE;
|
||||
|
||||
androidjni_attach_thread(&env);
|
||||
method_id = (*env)->GetMethodID(env, this->cls, "isConnected", "()Z");
|
||||
if (!method_id)
|
||||
{
|
||||
androidjni_exception_occurred(env);
|
||||
}
|
||||
else
|
||||
{
|
||||
connected = (*env)->CallBooleanMethod(env, this->obj, method_id);
|
||||
connected = !androidjni_exception_occurred(env) && connected;
|
||||
}
|
||||
androidjni_detach_thread();
|
||||
return connected;
|
||||
}
|
||||
|
||||
METHOD(network_manager_t, destroy, void,
|
||||
private_network_manager_t *this)
|
||||
{
|
||||
@@ -174,6 +196,7 @@ network_manager_t *network_manager_create(jobject context)
|
||||
.public = {
|
||||
.add_connectivity_cb = _add_connectivity_cb,
|
||||
.remove_connectivity_cb = _remove_connectivity_cb,
|
||||
.is_connected = _is_connected,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2013 Tobias Brunner
|
||||
* Copyright (C) 2012-2015 Tobias Brunner
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -65,6 +65,13 @@ struct network_manager_t {
|
||||
void (*remove_connectivity_cb)(network_manager_t *this,
|
||||
connectivity_cb_t cb);
|
||||
|
||||
/**
|
||||
* Check whether we currently have connectivity
|
||||
*
|
||||
* @return TRUE if currently connected
|
||||
*/
|
||||
bool (*is_connected)(network_manager_t *this);
|
||||
|
||||
/**
|
||||
* Destroy a network_manager_t instance
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2013 Tobias Brunner
|
||||
* Copyright (C) 2012-2015 Tobias Brunner
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -42,12 +42,21 @@ public class NetworkManager extends BroadcastReceiver
|
||||
mContext.unregisterReceiver(this);
|
||||
}
|
||||
|
||||
public boolean isConnected()
|
||||
{
|
||||
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo info = null;
|
||||
if (cm != null)
|
||||
{
|
||||
info = cm.getActiveNetworkInfo();
|
||||
}
|
||||
return info != null && info.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo info = cm.getActiveNetworkInfo();
|
||||
networkChanged(info == null || !info.isConnected());
|
||||
networkChanged(!isConnected());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user