php - How to listen to change in internet connection in my Android app? -
i created android app uses web service send , retrieve json data.
when make request while device online works fine when device goes offline app stuck , prints null pointer exception error.
is there way listen internet connection?
before request web service call method
private boolean isonline() { connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo ninfo = cm.getactivenetworkinfo(); if (ninfo != null && ninfo.isconnectedorconnecting()) return true; else return false; } if(isonline()){ // request service // make sure u have surrounded calling web-service try , catch try{ // here make request when connection go offline app catch error , ignore process }catch (exception e) { }
add permission
<uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.access_wifi_state" />
Comments
Post a Comment