Check Internet connection
For Example you making browser app then Checking intetnet connection
Follow This Steps:-
First Manifest :-
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="abu.webview">
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="abu.webview">
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
Activity_main:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="abu.webview.MainActivity">
-
- <android.support.v4.widget.SwipeRefreshLayout
- android:id="@+id/swipe"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <WebView
- android:id="@+id/webView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
-
- </android.support.v4.widget.SwipeRefreshLayout>
-
- </android.support.constraint.ConstraintLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="abu.webview.MainActivity">
- <android.support.v4.widget.SwipeRefreshLayout
- android:id="@+id/swipe"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <WebView
- android:id="@+id/webView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </android.support.v4.widget.SwipeRefreshLayout>
- </android.support.constraint.ConstraintLayout>
MainActivity:
- package abu.webview;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class MainActivity extends AppCompatActivity {
- private WebView webView;
private Boolean connected;
private String command;- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true);
- webView.getSettings().setAppCacheEnabled(true);
- intetnet("https://www.google.com");
- webView.setWebViewClient(new WebViewClient(){
- public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
- webView.loadUrl("file:///android_assets/error.html");
- }
- public void onPageFinished(WebView view, String url) {
- // do your stuff here
- }
- });
- }
- private void intetnet(final String url){
- check();
- if (connected){
webView.loadUrl(url);
} else {
neoistone.message(this,null,"No intetnet");
} - }
- private void check(){
- try{
command = "ping -c1 google.com";
connected = (Runtime.getRuntime().exec(command).waitFor() ==0);
}catch (Exception e){
eoistone.message(this,e,null);
} - }
- }
Any problem Contacts me:
whatsapp: group:- Join now
youtube channal:- go to help
0 Comments: