crash - Checking if a user is connected to the network in android -
in main activity have
if (isnetworkavailable() == true) { log.i("testing", "available"); } else { log.i("test", "unaivalibale"); }
then below have method
private boolean isnetworkavailable() { connectivitymanager connectivitymanager = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo activenetworkinfo = connectivitymanager.getactivenetworkinfo(); return activenetworkinfo != null && activenetworkinfo.isconnected(); }
i have set
<uses-permission android:name="android.permission.access_network_state" />
in manifest , yet when run tha application stops, killing application wrong
make class , add method properly:
public class connectivitystatus extends contextwrapper{ public connectivitystatus(context base) { super(base); } public static boolean isconnected(context context){ connectivitymanager manager = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo connection = manager.getactivenetworkinfo(); if (connection != null && connection.isconnectedorconnecting()){ return true; } return false; } }
get status, in activity using as:
if(!connectivitystatus.isconnected(your_class_name.this)){ //not connected }else { //connected }
Comments
Post a Comment