Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8146613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:03:52+00:00 2026-06-06T14:03:52+00:00

I have created appliction with webview. if i have do any action and the

  • 0

I have created appliction with webview. if i have do any action and the net is disconnected i want to display one alert. I have tried the following,

added this in oncreate method.

public class AndroidNetTestActivity extends Activity {

    public static WebView webview;
    private Handler mHandler = new Handler();       
    private boolean isConnected = true;
    final String offlineMessageHtml = "Net is disconnected";
    final String timeoutMessageHtml = "Connection timed out";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        webview=(WebView)findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true); 
        webview.loadUrl("file:///android_asset/www/index.htm");      
        webview.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
        isConnected=isNetworkAvailable();
        webview.setNetworkAvailable(isConnected);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                isConnected=isNetworkAvailable();
                if (isConnected) {
                    // return false to let the WebView handle the URL
                    return false;
                } else {
                    // show the proper "not connected" message
                    view.loadData(offlineMessageHtml, "text/html", "utf-8");
                    // return true if the host application wants to leave the current 
                    // WebView and handle the url itself
                    return true;
                }
            }
            @Override
            public void onReceivedError (WebView view, int errorCode, 
                String description, String failingUrl) {
                if (errorCode == ERROR_TIMEOUT) {
                    view.stopLoading();  // may not be needed
                    view.loadData(timeoutMessageHtml, "text/html", "utf-8");
                }
            }
        });
        webview.setWebChromeClient(new WebChromeClient());        
    }

    final class MyJavaScriptInterface
    {
        public void ProcessJavaScript(final String scriptname, final String args)
            {             
                mHandler.post(new Runnable()
                    {
                        public void run()
                            {
                                //ToDo
                            }
                    });
            }
    }  

    public boolean isNetworkAvailable() {
           Context context = getApplicationContext();
           ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
           if (connectivity == null) {
              //boitealerte(this.getString(R.string.alert),"getSystemService rend null");
           } else {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null) {
                 for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                       return true;
                    }
                 }
              }
           }
           return false;
        }
}

if i clicking on the log in button, it should show an error message if net is not available.

but it is not working. please check my code and tell me what i did wrong

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:03:53+00:00Added an answer on June 6, 2026 at 2:03 pm

    ok… i updated your code. just have a look

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.graphics.Bitmap;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.os.Handler;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    
    
    public class AndroidNetTestActivity extends Activity {
    
        public static WebView webview;
        private Handler mHandler = new Handler();       
        private boolean isConnected = true;
        final String offlineMessageHtml = "Net is disconnected";
        final String timeoutMessageHtml = "Connection timed out";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.full_screen_image_layout);
    
            webview=(WebView)findViewById(R.id.webview);
            webview.getSettings().setJavaScriptEnabled(true); 
            webview.loadUrl("http://www.google.com");      
            webview.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
            isConnected=isNetworkAvailable();
            webview.setNetworkAvailable(isConnected);
            webview.setWebViewClient(new WebViewClient() {
                /* (non-Javadoc)
                * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)
                */
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    System.out.println("page loading started");
                    // TODO Auto-generated method stub
                    if(!isNetworkAvailable2())
                    {
                        showInfoMessageDialog("network not available");
                        System.out.println("network not available");
                        return;
                    }
                    else System.out.println("network available");
    
                    super.onPageStarted(view, url, favicon);
    
                }
    
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    isConnected=isNetworkAvailable2();
                    if (isConnected) {
                        // return false to let the WebView handle the URL
                        return false;
                    } else {
                        // show the proper "not connected" message
                    // view.loadData(offlineMessageHtml, "text/html", "utf-8");
                        // return true if the host application wants to leave the current 
                        // WebView and handle the url itself
                        return true;
                    }
                }
                @Override
                public void onReceivedError (WebView view, int errorCode, 
                    String description, String failingUrl) {
                    if (errorCode == ERROR_TIMEOUT) {
                        view.stopLoading();  // may not be needed
                    // view.loadData(timeoutMessageHtml, "text/html", "utf-8");
                    }
                }
            });
            //webview.setWebChromeClient(new WebChromeClient());        
        }
    
        final class MyJavaScriptInterface
        {
            public void ProcessJavaScript(final String scriptname, final String args)
                {             
                    mHandler.post(new Runnable()
                        {
                            public void run()
                                {
                                    //ToDo
                                }
                        });
                }
        }  
    
        private void showInfoMessageDialog(String meaasge)
        {
            AlertDialog alertDialog = new AlertDialog.Builder(
                    AndroidNetTestActivity.this).create();
            alertDialog.setTitle("Connectivity");
            alertDialog.setMessage(meaasge);
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            //alertDialog.setIcon(R.drawable.error);
            alertDialog.show();
        }
    
        private boolean isNetworkAvailable2()
        {
            System.out.println("isNetworkAvailable2 called");
            NetworkInfo info = (NetworkInfo) ((ConnectivityManager) getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE))
                    .getActiveNetworkInfo();
    
            if (info == null || !info.isAvailable() || !info.isConnected())
                return false;
            else return true;
        }
    
    
        public boolean isNetworkAvailable() {
            Context context = getApplicationContext();
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity == null) {
                //boitealerte(this.getString(R.string.alert),"getSystemService rend null");
            } else {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                        }
                    }
                }
            }
            return false;
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom listview that have one webview per element. My problem
We have created an application which we want to run in a 64 bit
I have created one application in flex that is accessing the Java webservice using
I have created an application that loads a website using WebView. Everything works fine
I am new to iphone development.I have created a view based application.Now i want
I have Created a mail server using pantomime framework.My Appliction is running only in
I have created a webview application to run a web application on an android
I want to create a WebView to display an image. But on the second/third
I have created one application which is reading outlook mail, but when user install
What I want to do... I have a webview in my android app. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.