Android app - using AsyncTask - stops opening a URL in WebView, it is opened by the default browser if I call another URL -


i'm beginning develop in android using material http://developer.android.com.

i took 1 of examples , modified it, application can connect webpage. works when opened if click on actionbar item should open page new page isn't opened in webview, it's launched default browser.

i tried in many way understand how avoid that, little experience didn't allow me fixe problem.

can me?

many thanks. nino

public class mainactivity extends activity {     public static final string wifi = "wi-fi";     public static final string = "any";     public static string pagina ="http://www.kibao.org/simu/wap.php?lng=";         public static string base ="http://www.kibao.org";     public static string attuale ="";     public static string lng = "";     final context context = this;      private static boolean wificonnected = false;     private static boolean mobileconnected = false;     public static boolean refreshdisplay = true;     public static string spref = null;      public static string pagina = "";      private networkreceiver receiver = new networkreceiver();     @override     public void oncreate(bundle savedinstancestate) {         lng = getresources().getstring(r.string.lng);         intentfilter filter = new intentfilter(connectivitymanager.connectivity_action);         receiver = new networkreceiver();         this.registerreceiver(receiver, filter);          super.oncreate(savedinstancestate);         setcontentview(r.layout.main);     }      @override     public void onstart() {         super.onstart();          sharedpreferences sharedprefs = preferencemanager.getdefaultsharedpreferences(this);          spref = sharedprefs.getstring("listpref", "wi-fi");          updateconnectedflags();           if (refreshdisplay) {             attuale=pagina.concat(lng);             loadpage(attuale);         }     }     @override     public void ondestroy() {         super.onstop();         string ciao = getresources().getstring(r.string.ciao);         show_toast(ciao);         if (receiver != null) {            this.unregisterreceiver(receiver);         }     }     private void updateconnectedflags() {         connectivitymanager connmgr =             (connectivitymanager) getsystemservice(context.connectivity_service);          networkinfo activeinfo = connmgr.getactivenetworkinfo();         if (activeinfo != null && activeinfo.isconnected()) {             wificonnected = activeinfo.gettype() == connectivitymanager.type_wifi;             mobileconnected = activeinfo.gettype() == connectivitymanager.type_mobile;         } else {             wificonnected = false;             mobileconnected = false;         }     }      private void loadpage(string pgurl) {         if (((spref.equals(any)) && (wificonnected || mobileconnected))             || ((spref.equals(wifi)) && (wificonnected))) {             new downloadwebpagetask().execute(pgurl);         } else {             showerrorpage();         }     }     // displays error if app unable load content.    private void showerrorpage() {         ....     }     @override    public boolean oncreateoptionsmenu(menu menu) {         super.oncreateoptionsmenu(menu);         menuinflater inflater = getmenuinflater();         inflater.inflate(r.menu.items, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {      // handle item selection     switch (item.getitemid()) {     case r.id.menu1:         if (refreshdisplay) {              attuale=base.concat("/partite.php?lng=");             attuale=attuale.concat(lng);             loadpage(attuale);         }         return true;     ....     default:         return super.onoptionsitemselected(item);     }      }     private inputstream downloadurl(string urlstring) throws ioexception {         url url = new url(urlstring);         httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setreadtimeout(10000 /* milliseconds */);         conn.setconnecttimeout(15000 /* milliseconds */);         conn.setrequestmethod("get");         conn.setdoinput(true);         conn.connect();         inputstream stream = conn.getinputstream();         return stream;    }      public class networkreceiver extends broadcastreceiver {         @override         public void onreceive(context context, intent intent) {            connectivitymanager connmgr =                 (connectivitymanager) context.getsystemservice(context.connectivity_service);             networkinfo networkinfo = connmgr.getactivenetworkinfo();             if (wifi.equals(spref) && networkinfo != null                 && networkinfo.gettype() == connectivitymanager.type_wifi) {                 refreshdisplay = true;                 toast.maketext(context, r.string.wifi_connected, toast.length_short).show();             } else if (any.equals(spref) && networkinfo != null) {                 refreshdisplay = true;             } else {                 refreshdisplay = false;                 toast.maketext(context, r.string.lost_connection, toast.length_short).show();             }         }     }      private class downloadwebpagetask extends asynctask<string, void, string> {          @override         protected string doinbackground(string... urls) {             try {             return loadwebpagefromnetwork(urls[0]);             } catch (ioexception e) {                 return getresources().getstring(r.string.connection_error);             }         }          @override         protected void onpostexecute(string result) {             setcontentview(r.layout.main);             webview mywebview = (webview) findviewbyid(r.id.webview);             mywebview.loadurl(attuale);                     }     }     private string loadwebpagefromnetwork(string urlstring) throws ioexception {         inputstream stream = null;         try {             stream = downloadurl(urlstring);             pagina = getstringfrominputstream(stream);         } {             if (stream != null) {                 stream.close();             }         }     return pagina;     }     private static string getstringfrominputstream(inputstream is) {         bufferedreader br = null;         stringbuilder sb = new stringbuilder();          string line;         try {          br = new bufferedreader(new inputstreamreader(is));         while ((line = br.readline()) != null) {             sb.append(line);         }      } catch (ioexception e) {         e.printstacktrace();     } {         if (br != null) {             try {                 br.close();             } catch (ioexception e) {                 e.printstacktrace();             }         }     }      return sb.tostring();  }         ........ } 

this downloadwebpagetask tries "download" webpage string, using webview. not correct way it. using wrong tutorial. don't use asynctasks this. start over, using this:

http://developer.android.com/reference/android/webkit/webview.html

or

http://www.mkyong.com/android/android-webview-example/

simply call webview.loadurl(url) again different url if want load different page. it's simple that.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -