Convert Website Into An Android App | #4 Check User Is Connected To The Internet

 1. User Internet เคธे connected เคนै เคฏा เคจเคนीं เค‰เคธเค•ो Check เค•เคฐเคจे เค•े เคฒिเค เค†เคชเค•ो เคธเคฌเคธे เคชเคนเคฒे app > manifests > AndroidManifest.xml เคฎें เคœाเคจा เคนै!

2. เคซिเคฐ เคจीเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎें เคฆिเค–ाเค เคนुเค เคธ्เคฅाเคจ เคชเคฐ Paste เค•เคฐ เคฒेเคจा เคนै!

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>



 3. เค…เคฌ เค†เคชเค•ो MainActivity.java   (app > java > Package Name > MainActivity.java)  เคฎें เคœाเคจा เคนै เคซिเคฐ เคจीเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎें เคฆिเค–ाเค เค…เคจुเคธाเคฐ Package Name เค•ो เค›ोเฅœเค•เคฐ เคฌाเค•ि เคธเคญी Code เค•े เคธ्เคฅाเคจ เคชเคฐ Paste เค•เคฐ เคฆेเคจा เคนै!

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

String websiteURL = "https://youtube.com/technicalsangrah"; // sets web url
private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
{
//if there is no internet do this
setContentView(R.layout.activity_main);
//Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();

new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("No internet connection available")
.setMessage("Please Check you're Mobile data or Wifi network.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
//.setNegativeButton("No", null)
.show();

}
else
{
//Webview stuff
webview = findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl(websiteURL);
webview.setWebViewClient(new WebViewClientDemo());

}
}


private class WebViewClientDemo extends WebViewClient {
@Override
//Keep webview in app when clicking links
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}


}

class CheckNetwork {

private static final String TAG = CheckNetwork.class.getSimpleName();

public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}

}
}
}



เค…เคฌ User เค•ा Internet Connection On เคจเคนीं เคฐเคนेเค—ा เคคो เค‰เคจเค•ो "Please check you're Mobile data or Wifi network" เค•เคฐเค•े เค›ोเคŸा เคธा Warning show เคนो เคœाเคฏा เค•เคฐेเค—ा!

Comments

  1. bhai app open ho jane ke bad network lost hone par webpage not available show kr rha h

    ReplyDelete

Post a Comment

Popular posts from this blog

Convert Website Into An Android App | #10 Set Up Push Notifications

Convert Website Into An Android App | #6 Add Swipe down to Refresh functionality

Convert Website Into An Android App | #8 Adding a Splash Screen