1

I am beginner in android development. Below i give my main activity.java code. My requirement if internet connection or wifi not working on mobile devices then following thing perform

  1. If internet connection not working then show the my splash screen backrgound on start of application and give the dialogue box or alert like this screenshot of trip advisor Check this image and screenshot. If cancel button press then dialogue box disappear and only display splash screen and if press try again then try to perform connect to internet.

In my code i take the code buy dialogue box, i not want buy function this is not my requirement. i want exactly like trip advisor. For me easy if somebody edit the my code and show where make changes very thanks in advance

package com.example.edarabia;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     mywebview = (WebView) findViewById(R.id.webview);
    if(isNetworkConnected() == true){
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new myWebClient());
        mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    mywebview.getSettings().setBuiltInZoomControls(true);
    mywebview.getSettings().setLoadWithOverviewMode(false);
    mywebview.getSettings().setUseWideViewPort(false);
    }else{
    showBuyDialog();
    }
}






//  @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
//          mywebview.goBack();
//            return true;
 //       }

 //       return super.onKeyDown(keyCode, event);

//  }

 // To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    finish();
}


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        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;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("just for testing");

    builder.setMessage("Check you net conectivity....");
    builder.setCancelable(false);
    builder.setPositiveButton("Buy", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent browserIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://www.google.com"));
            startActivity(browserIntent);
        }
    });
    builder.show();
}




}

3 Answers3

1

This might be helpful: Detect whether there is an Internet connection available on Android

or try digging into BroadcastReceivers. Sample code:

public class InternetConnectionStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        _v("Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                _v("Network " + ni.getTypeName() + " connected");
                onNetworkConnection(context, true);
            }
        }
        if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            _v("There's no network connectivity");
            onNetworkConnection(context, false);
        }
    }

    private void onNetworkConnection(Context context, boolean isConnected) {
          //show dialog
    }

}
Community
  • 1
  • 1
agamov
  • 4,407
  • 1
  • 27
  • 31
0

I got the solution and below the updated code

package com.example.edarabia;

import android.annotation.SuppressLint;
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.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(isNetworkConnected() == true){
    setContentView(R.layout.activity_main);
    mywebview = (WebView) findViewById(R.id.webview);       
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new myWebClient());
    mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    mywebview.getSettings().setBuiltInZoomControls(true);
    mywebview.getSettings().setLoadWithOverviewMode(false);
    mywebview.getSettings().setUseWideViewPort(false);
    }else{
    setContentView(R.layout.splash);    
    showBuyDialog();
    }
}






//  @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
//          mywebview.goBack();
//            return true;
 //       }

 //       return super.onKeyDown(keyCode, event);

//  }

 // To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    finish();
}


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        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;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");

    builder.setMessage("You Must have an Internet connection to use Edarabia. Please connect and try again.");
    builder.setCancelable(false);
    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            finish(); 
        }
    });
    builder.show();
}




}
0

Try to replace showButDialog() method,

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");   
    builder.setMessage("You must have an Internet connection to use TripAdvisor. Please connect and try again.");
    builder.setCancelable(false);

    builder.setPositiveButton("Try again", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int id) {
               isNetworkConnected();
         }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.show();
}

And to get the UI same as in the picture, add following line before application tag, like this,

<uses-sdk android:targetSdkVersion="15" />

This kind of dialog is available from Android OS 3.0 (Honey Comb) and above. So you cannot see dialog in previous versions.

RootCode
  • 569
  • 7
  • 12