For Example you making browser app then Checking intetnet connection Follow This Steps:- First Manifest :- <?xml version= "1.0"...

Check Internet connection


For Example you making browser app then Checking intetnet connection
Follow This Steps:-



First Manifest :-


  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     package="abu.webview">  

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

  5.     <application  

  6.         android:allowBackup="true"  

  7.         android:icon="@mipmap/ic_launcher"  

  8.         android:label="@string/app_name"  

  9.         android:roundIcon="@mipmap/ic_launcher_round"  

  10.         android:supportsRtl="true"  

  11.         android:theme="@style/AppTheme">  

  12.         <activity android:name=".MainActivity">  

  13.             <intent-filter>  

  14.                 <action android:name="android.intent.action.MAIN" />  

  15.   

  16.                 <category android:name="android.intent.category.LAUNCHER" />  

  17.             </intent-filter>  

  18.         </activity>  

  19.     </application>  


Activity_main:



  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  

  4.     xmlns:tools="http://schemas.android.com/tools"  

  5.     android:layout_width="match_parent"  

  6.     android:layout_height="match_parent"  

  7.     tools:context="abu.webview.MainActivity">  

  8.   

  9.     <android.support.v4.widget.SwipeRefreshLayout  

  10.         android:id="@+id/swipe"  

  11.         android:layout_width="match_parent"  

  12.         android:layout_height="match_parent">  

  13.   

  14.         <WebView  

  15.             android:id="@+id/webView"  

  16.             android:layout_width="match_parent"  

  17.             android:layout_height="match_parent"/>  

  18.   

  19.     </android.support.v4.widget.SwipeRefreshLayout>  

  20.   

  21. </android.support.constraint.ConstraintLayout> 


MainActivity:

  1. package abu.webview;  
  2.   
  3. import android.support.v4.widget.SwipeRefreshLayout;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.webkit.WebView;  
  7. import android.webkit.WebViewClient;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.   
  11. private WebView webView;
  12. private Boolean connected;
    private String command;
  13.   
  14.    @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.             
  19.        webView = (WebView) findViewById(R.id.webView);       webView.getSettings().setJavaScriptEnabled(true);  
  20.      webView.getSettings().setAppCacheEnabled(true);  
  21.     intetnet("https://www.google.com");
  22.      
  23.    webView.setWebViewClient(new WebViewClient(){  
  24.   
  25.             public void onReceivedError(WebView view, int errorCode, String description,                 String failingUrl) {  
  26.   
  27.                 webView.loadUrl("file:///android_assets/error.html");  
  28.   
  29.             }  
  30.   
  31.             public void onPageFinished(WebView view, String url) {  
  32.                 // do your stuff here  
  33.             }  
  34.   
  35.         });  
  36.    }
  37.   private void intetnet(final String url){
  38.     check();
  39.    if (connected){
               webView.loadUrl(url);
    } else {
    neoistone.message(this,null,"No intetnet");
    }
  40.      }
  41.   private void check(){
  42.     try{
        command = "ping -c1 google.com";
    connected = (Runtime.getRuntime().exec(command).waitFor() ==0);
    }catch (Exception e){
    eoistone.message(this,e,null);
    }
  43.   }
  44. }


Any problem Contacts me:

whatsapp: group:- Join now 
youtube channal:- go to help

0 Comments: