0

My app is based on a fullscreen WebView and I wanted to show a local file if there is no internet connection, otherwise load my website. I never used AsyncTask before and tried the following:

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Check Internet Connection
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    HostAvailabilityTask hostAvailable = new HostAvailabilityTask(this);
    boolean online = hostAvailable.isOnline();

    if (!online) {
        // Loading local html file into web view
        mWebView.loadUrl("file:///android_asset/sample.html");
    } else {
        // load my website here

HostAvailabilityTask:

public class HostAvailabilityTask extends AsyncTask<String, Void, Boolean> {

    private MainActivity main;

    public HostAvailabilityTask(MainActivity main) {
        this.main = main;
    }

    protected Boolean doInBackground(String... params) {
        return isOnline(); // Correct way using AsyncTask?
    }

    protected void onPostExecute(Boolean... result) {
        if(!result[0]) {
            return; // What to return?
        }
    }

    public boolean isOnline() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int     exitValue = ipProcess.waitFor();
            return (exitValue == 0);
        }
        catch (IOException e)          { e.printStackTrace(); }
        catch (InterruptedException e) { e.printStackTrace(); }

        return false;
    }
}

As you can see I'm just calling the method isOnline(); inside the MainActivity and think this is the wrong way to use a AsyncTask? I just want to make sure to do it "the right way". I also don't know what would be logical to return in onPostExecute in that case?

As stated before I never used AsyncTask, so I hope someone could help me out. I also commented some lines in my code to make my confusion clear.

Asif Patel
  • 1,744
  • 1
  • 20
  • 27
AlexioVay
  • 4,338
  • 2
  • 31
  • 49
  • You can't call `isOnline` directly. You have to use the `execute` method instead. Here is an example how to use it http://stackoverflow.com/questions/9671546/asynctask-android-example – Tesseract Mar 23 '17 at 21:01
  • @SpiderPig Thanks, I visited that question already. I just wanted to know if this is applicable to my specific case and if I'm doing it right. Because there are also other question not loading webview with AsyncTask, but I don't know if this is related to my question since I'm just checking the internet connection: http://stackoverflow.com/questions/14122089/webview-with-asynctask-on-android – AlexioVay Mar 23 '17 at 21:05
  • In your case your asynctask is not doing anything with the webview so that other question you mentioned is not relevant here. But why do you even want to run this in a different thread? Also I'm not sure if ping is the best way to perform this check. You could also try this http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android – Tesseract Mar 23 '17 at 21:13
  • But then again it would make more sense to always try to load your website and if that fails you use the local file. – Tesseract Mar 23 '17 at 21:15
  • I browsed through a lot of questions here and I also read about checking the Network Availability, but there can be a WiFi connection without being connected to the internet. So I used the code of some other answer. And I tried to implement the AsyncTask because it was also suggested in some answer, but it's difficult to know the best practice as a Java noob. – AlexioVay Mar 23 '17 at 21:18
  • But your website could be unaccessible even if there is an internet connection. So it might fail even if the ping succeeds. Anyway I don't see why you would need to use AsyncTask unless your app needs to do something else at the same time it checks for internet access. – Tesseract Mar 23 '17 at 21:23
  • Okay. Thank you, I would try to implement the WebView onReceivedError method then. If you would post that as an answer I'd accept it. – AlexioVay Mar 23 '17 at 21:24

1 Answers1

0
 @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
           //Your code to do
        Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
    }