java - Android call a check for internet connection automatically -
i'd know if there's mechanism whenever try use internet, somehow automatically behind scences method (isnetworkavailable) called.
the question not how check internet connection know that:
public boolean isnetworkavailable(){ connectivitymanager cm = (connectivitymanager)ctx.getsystemservice(context.connectivity_service); networkinfo activenetwork = cm.getactivenetworkinfo(); boolean isconnected = activenetwork != null && activenetwork.isconnectedorconnecting(); return isconnected; }
to clarify myself, want prevent calling isnetworkavailable() each time use internet connection (either classes call explicitly or third party classes use opens internet connections themselves), code won't filled everywhere with:
if(isnetworkavailable()){ // send message server or open other internet connection }
but function somehow called before each attempt connect internet.
hope i've cleared myself. couldn't find solution on so.
p.s. app can run on offline mode, don't want simple broadcast reciever blocks app whenever there's no internet connection (like apps do).
i have used broadcastreceiver, check internet connectivity automatically.
networkutil.java
public class networkutil { public static final int not_connected = 0; public static final int wifi = 1; public static final int mobile = 2; public static int getconnectionstatus(context context) { connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo activenetwork = cm.getactivenetworkinfo(); if (activenetwork != null) { if (activenetwork.gettype() == connectivitymanager.type_wifi) return wifi; if (activenetwork.gettype() == connectivitymanager.type_mobile) return mobile; } return not_connected; } }
networkconnectivitycheck.java
public class networkconnectivitycheck { public boolean internetavailable = false; private broadcastreceiver networkchangereceiver; public networkconnectivitycheck() { this.networkchangereceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { int networkstate = networkutil.getconnectionstatus(context); if (networkstate == networkutil.not_connected) { log.i("internetapplication", "internet not available."); internetavailable = false; getinternetstatus(); } else if (networkstate == networkutil.mobile) { log.i("internetapplication", "internet available through mobile data."); internetavailable = true; getinternetstatus(); } else if (networkstate == networkutil.wifi) { log.i("internetapplication", "internet available through wifi."); internetavailable = true; getinternetstatus(); } } }; } public boolean getinternetstatus(){ log.d("internetapplication", "value of internet " + string.valueof(internetavailable)); return this.internetavailable; } public void register(context context){ context.registerreceiver(networkchangereceiver, new intentfilter("android.net.conn.connectivity_change")); } public void unregister(context context){ context.unregisterreceiver(networkchangereceiver); } }
after these 2 files, initialise class before oncreate method , this.
mainactivity.java
//internet connectivity check private networkconnectivitycheck status = new networkconnectivitycheck(); public void networkstatuscheck(){} @override protected void oncreate(bundle savedinstancestate) { networkstatuscheck(); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_sample); }
Comments
Post a Comment