Check For Internet Connection in android -


i'm developing application check internet available or not...

i'm getting this.

here connection class :

  public class chechconnection {  private context _context;  public chechconnection(context context){     this._context = context; }  public boolean isconnectingtointernet(){     connectivitymanager connectivity = (connectivitymanager) _context.getsystemservice(context.connectivity_service);       if (connectivity != null)        {           networkinfo[] info = connectivity.getallnetworkinfo();           if (info != null)                (int = 0; < info.length; i++)                    if (info[i].getstate() == networkinfo.state.connected)                   {                       return true;                   }        }       return false; } } 

and using code checking : code recharge activity class

chechconnection cdetactor; boolean isinternetpresent = false; 

if click on button should display if there internet connection.

 cdetactor=new chechconnection(getapplicationcontext());  isinternetpresent = cdetactor.isconnectingtointernet();      btn_recharge.setonclicklistener(new onclicklistener() {      @override     public void onclick(view v) {          if (isinternetpresent){         toast.maketext(mcontext,"button pressed",toast.length_long).show();         }         else             alert.showalertdialog(mcontext,"check connection","check connection setting",false);     } }); 

this own dialog manager :

public class alertdialogmanager {

public void showalertdialog(final context context, string title, string message,         boolean status) {     final dialog alertdialog = new dialog(new contextthemewrapper(context, android.r.style.theme_translucent));     alertdialog.requestwindowfeature(window.feature_no_title);      alertdialog.setcancelable(true);     alertdialog.setcontentview(r.layout.dialog);     alertdialog.settitle(title);     button ok=(button) alertdialog.findviewbyid(r.id.btncancel);    button cancel=(button) alertdialog.findviewbyid(r.id.btnsearch);    ok.setonclicklistener(new onclicklistener() {      @override     public void onclick(view v) {          activity activity=(activity) context;         activity.finish();     } });    cancel.setonclicklistener(new onclicklistener() {      @override     public void onclick(view v) {         alertdialog.dismiss();     } });      alertdialog.show(); } 

}

but if there internet connection, gives me error. please check logcat value :

04-29 11:26:15.011: e/androidruntime(2177): process: com.example.lifegoal, pid: 2177 04-29 11:26:15.011: e/androidruntime(2177): java.lang.nullpointerexception: attempt invoke virtual method 'void com.lifegoal.eshop.helper.alertdialogmanager.showalertdialog(android.content.context, java.lang.string, java.lang.string, java.lang.boolean)' on null object reference 04-29 11:26:15.011: e/androidruntime(2177):     @ com.lifegoal.eshop.recharge_activity$1.onclick(recharge_activity.java:51) 

but if there internet connection, goes else part , gives me logcat value

thanks!

use method :

public static boolean isdeviceonline(context context) {         boolean isconnectionavail = false;         try {             connectivitymanager cm = (connectivitymanager) context                     .getsystemservice(context.connectivity_service);             networkinfo netinfo = cm.getactivenetworkinfo();             if(netinfo != null)             return netinfo.isconnected();             else                  return isconnectionavail;         } catch (exception e) {             e.printstacktrace();         }         return isconnectionavail;     } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -