Skip to content

Commit caff228

Browse files
committed
Merge pull request kivy#65 from legikaloz/master
Get Android network state by direct query or with BroadcastReciever
2 parents 9b8d4fa + ebf5ce4 commit caff228

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 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,48 @@ public static String scanWifi() {
232234
return "";
233235
}
234236

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

0 commit comments

Comments
 (0)