java - How to put this piece of code into an AsyncTask or a Thread -
im new in threads , asynctask's , know how can run following code in background:
i have method checks if have active internet conection, im trying show progressdialog before calling method , dismissing dialog when succeed or fail:
oncreate:
progress = progressdialog.show(this, "dialog title","dialog message", true); if(isonline()){}else{}
my method.
public boolean isonline() { try { process p1 = java.lang.runtime.getruntime().exec("ping -c 1 www.karlol.com"); int returnval = p1.waitfor(); boolean reachable = (returnval==0); canshowview = true; progress.dismiss(); return reachable; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return false; }
the method works (it tells me if there's active internet connection) problem alertdialog not working to, im pretty sure if put thread or asyntask should work want.
update: use connectivitymanager check if user connected network, not verify connection
private boolean isnetworkavailable() { connectivitymanager connectivitymanager = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo activenetworkinfo = connectivitymanager.getactivenetworkinfo(); return activenetworkinfo != null && activenetworkinfo.isconnected(); }
thanks.
i dont think best way result looking here is
(asynctask<string,string,boolean>(){ protected boolean doinbackground(string... params) { try { process p1 = java.lang.runtime.getruntime().exec("ping -c 1 www.karlol.com"); int returnval = p1.waitfor(); boolean reachable = (returnval==0); canshowview = true; progress.dismiss(); return reachable; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return false } protected void onpostexecute(boolean result) { /// here super.onpostexecute(result); } }).execute()
i @ using better solution problem
public static boolean isnetworkavailable(context context) { connectivitymanager connectivity = (connectivitymanager) context .getsystemservice(context.connectivity_service); if (connectivity == null) { return false; } else { networkinfo[] info = connectivity.getallnetworkinfo(); if (info != null) { (int = 0; < info.length; i++) { if (info[i].getstate() == networkinfo.state.connected) { return true; } } } } return false; }
Comments
Post a Comment