Note that there are some explanatory texts on larger screens.

plurals
  1. POTips with AsyncTask cleanup and usage Android
    text
    copied!<p>This is the first time I used AsyncTask and it works but it's very dirty as I just copied and pasted some code. I want to do it the right way and as clean as possible. So can anyone help me with cleaning up my code and tell me how to do it the right way? You would do me a great favor in helping me and giving me more experience in future Android developing. I want to learn it the right way from the start ;)</p> <p><strong>EDIT:</strong> After reading a bunch of tutorials and watching video's I think I made an huge improvement! And I'm very happy I did it! But there's only one small problem left. At the startup of the app, it doesn't load the page. The shouldOverrideUrlLoading works great after I clicked a link but at the startup it shows only a blank screen. What's the problem?</p> <pre><code>public class WebviewActivity extends MainActivity { private WebView myWebView; private ProgressDialog progressDialog; private boolean mConnection = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ScrollView mainScrollView = (ScrollView)findViewById(R.id.ScrollView01); myWebView = (WebView)findViewById(R.id.webview); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); myWebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { new checkConnection().execute(); if (mConnection == true){ view.loadUrl(url); } return true; } @Override public void onPageFinished(WebView view, String url) { mainScrollView.fullScroll(ScrollView.FOCUS_UP); } }); myWebView.requestFocus(View.FOCUS_DOWN); myWebView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!v.hasFocus()) { v.requestFocus(); } break; } return false; } }); } @Override public void onResume() { super.onResume(); new checkConnection().execute(); if (mConnection == true){ myWebView.loadUrl(webLink); } } //------------------------------------------------------ //-----DOING THE CONNECTION CHECK IN ANOTHER THREAD----- //------------------------------------------------------ public class checkConnection extends AsyncTask&lt;Void, Void, Void&gt;{ int mStatusCode = 0; Exception mConnectionException; @Override protected void onPreExecute(){ super.onPreExecute(); progressDialog = ProgressDialog.show(WebviewActivity.this, "", "Loading...", true); progressDialog.show(); } @Override protected Void doInBackground(Void... params) { try { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) { URL url = new URL(webLink); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds urlc.connect(); mStatusCode = urlc.getResponseCode(); if (mStatusCode == 200){ //Nothing to do. } } } catch (IOException e) { e.printStackTrace(); mConnectionException = e; } return null; } @Override protected void onPostExecute(Void param){ progressDialog.dismiss(); if (mStatusCode == 200){ mConnection = true; } else if (mStatusCode == 404){ myWebView.loadUrl("file:///android_asset/errorpage404.html"); } else { myWebView.loadUrl("file:///android_asset/errorpage.html"); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload