Skip to content

Commit 4f0363d

Browse files
committed
Get Android network state by direct query or with intent
1 parent 0cf6e14 commit 4f0363d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/src/org/renpy/android/Hardware.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import android.content.BroadcastReceiver;
1717
import android.content.Intent;
1818
import android.content.IntentFilter;
19+
import android.net.ConnectivityManager;
20+
import android.net.NetworkInfo;
1921

2022
/**
2123
* Methods that are expected to be called via JNI, to access the
@@ -232,4 +234,52 @@ public static String scanWifi() {
232234
return "";
233235
}
234236

237+
/**
238+
* network state, coarse
239+
*/
240+
241+
public static boolean network_state = false;
242+
243+
/**
244+
* To recieve network state changes
245+
*/
246+
public static void registerNetworkCheck()
247+
{
248+
IntentFilter i = new IntentFilter();
249+
i.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
250+
context.registerReceiver(new BroadcastReceiver() {
251+
252+
@Override
253+
public void onReceive(Context c, Intent i) {
254+
checkNetwork();
255+
}
256+
257+
}, i);
258+
}
259+
260+
261+
/**
262+
* Check network state directly
263+
*
264+
* (only one connection can be active at a given moment, detects al type of networks)
265+
*
266+
*/
267+
public static boolean checkNetwork()
268+
{
269+
boolean state = false;
270+
final ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
271+
272+
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
273+
if (activeNetwork != null && activeNetwork.isConnected()) {
274+
network_state = true;
275+
} else {
276+
network_state = false;
277+
}
278+
279+
return state;
280+
}
281+
282+
283+
284+
235285
}

0 commit comments

Comments
 (0)