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 😉
EDIT:
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?
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<Void, Void, Void>{
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");
}
}
}
}
Please, don’t call
progressDialog.dismiss();fromdoInBackground(), useonPostExecute()instead.doInBackground()is not an UI thread, so trying to manipulate UI elements might give you wild consequences.Here’s why you don’t get your page loaded on the first try:
(1) starts asynchronous task, which is going to be finished some time in the future
(2) tries to check the results of (1) immediately, so there are no results yet.
here’s modified version of your
AsyncTask, which accepts url as a parameter: