I am developing small app where I am calling webpage.
I override WebViewClient as I wants to open webpage in same view.
My problem is if there is any problem o
Maybe you can give your own BroadcastReceiver class a shot.
Try it like this (totally untested):
1.) GlobalState class:
public GlobalState {
public static boolean isOnline = true;
public static String lastUrl = "";
}
2.) Save your last url and connection-state in onReceivedError:
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
GlobalState.isOnline = false;
GlobalState.lastUrl = failingUrl;
hideErrorPage(view);
}
3.) Implement a callback in your activity class (i do not know the name, so i called it MainActivity.java):
public class MainActivity extends Activity implements IConnectionCallback {
private ConnectionBroadReceiver cbr = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
//some code...
cbr = new ConnectionBroadReceiver (this);
registerReceiver(cbr, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
//some more code...
}
//this method will be triggered by ConnectionBroadCastReceiver
@Override
public void reload() {
//now reload your webview:
questionweb.loadUrl(GlobalState.lastUrl);
}
}
4.) Define your Interface callback:
public interface IConnectionCallback {
public void reload();
}
5.) Last but not least the ConnectionBroadReceiver class:
public class ConnectionBroadReceiver extends BroadcastReceiver {
private IConnectionCallback callback = null;
public ConnectionBroadReceiver (IConnectionCallback callback) {
this.callback = callback;
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
// lets check the connection
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//when last state was the offline state (GlobalState.isOnline== false),
//lets trigger the callback
if (GlobalState.isOnline == false) {
callback.reload();
}
GlobalState.isOnline = true;
} else {
GlobalState.isOnline = false;
}
}