0

I have created a webview and loaded a URL.

If the network connection is not available it loads a custom a URL with a retry button.

  1. How can have this button reload this activity after user has network connection? (If this is easier by calling another activity and then calling this webview again then that works as well.)

  2. Have I handled the Is the network connection not available code correctly?

    Below is my code :

     import android.net.ConnectivityManager; 
     import android.net.NetworkInfo; 
     import android.os.Bundle; 
     import android.app.Activity; 
     import android.view.Menu; 
     import android.view.MenuItem; 
     import android.webkit.WebView; 
     import android.webkit.WebViewClient; 
     import android.widget.Toast; 
     import android.support.v4.app.NavUtils; 
     import android.annotation.TargetApi; 
     import android.content.Context; 
     import android.os.Build;
    
    
    public class WebActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
    
    
        if(isNetworkStatusAvailable (getApplicationContext())) {
    
            Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_SHORT).show();
            WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.loadUrl("http://www.bing.com");
    
            myWebView.setWebViewClient(new WebViewClient() {
    
                @Override
                public void onReceivedError(WebView view, int errorCode,
                        String description, String failingUrl) {
                    Toast.makeText(getApplicationContext(), "Internet Connection Unavailable Or " + description , Toast.LENGTH_LONG).show();
                    view.loadUrl("file:///android_asset/mob.html");
                    super.onReceivedError(view, errorCode, description, failingUrl);
                }
             });
    
        } else {
    
            Toast.makeText(getApplicationContext(), "Internet Connection Unavailable.", Toast.LENGTH_LONG).show();
            WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.loadUrl("file:///android_asset/mob.html");
    
    
         }
    
        // Show the Up button in the action bar.
        setupActionBar();
    }
    
    public static boolean isNetworkStatusAvailable (Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) 
        {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if(netInfos != null)
            if(netInfos.isConnected()) 
                return true;
        }
        return false;
    }
    
    }
    
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
GagaBytes
  • 15
  • 8

2 Answers2

0

call webview.reload() inside your Button oncClickListener to reload your url.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
  • when there is no network available it redirects to a custom html page. my button is on a html page, i cannot call the above. Also i am looking to call the activity again. – GagaBytes Mar 24 '14 at 11:07
  • ok. you may find this post usefull: http://stackoverflow.com/questions/5907439/get-the-click-event-from-webpage-in-my-android-application. By the way, you are checking connectivity the right way :D – joao2fast4u Mar 24 '14 at 14:54
  • Thanks a lot. I will go through that post and see if it helps me. I think i will create a new post as well explaining better how i want an interface when you use play store app without network connectivity. – GagaBytes Mar 25 '14 at 06:52
0

I have now solved this using the following method, i replaced the code inside else with :

Intent errorIntent = new Intent(this, NetworkErrorActivity.class);
startActivity(errorIntent); 
finish();

In my new activity i call my previous activity again by pressing a button.

GagaBytes
  • 15
  • 8