DEVELOPMENT

Android application running with internet must perform a check to know that the same is available or not. Here, i'm sharing the code to perform the required task.

internet connection

THE SITUATION

If the net is available on the device, a text message saying ""Internet Connection Found"" get displayed on the screen otherwise application is closed.

so lets..start...

BUILD.GRADLE
apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.tut.tut_7_1"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

ANDROIDMANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tut.tut_7_1" >
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
</manifest> 

<uses-permission> tag are used to include the permission required by the application.
MAINACTIVITY.JAVA
package com.example.tut.tut_7_1;
     
        import android.app.Activity;
        import android.content.Context;        
        import android.net.ConnectivityManager;
        import android.net.NetworkInfo;
        import android.os.AsyncTask;
        import android.os.Bundle;     
        import android.view.View;
        import android.widget.ProgressBar;
        import android.widget.TextView;

public class MainActivity extends Activity {

    private ProgressBar pb;
    private TextView disp;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb=(ProgressBar)findViewById(R.id.progressBar1);
        new MyAsyncTask().execute();

    }


    private class MyAsyncTask extends AsyncTask{
        boolean conn;
        String text;

        @Override
        protected String doInBackground(String... params) {
          
            conn=checkconn();

            if(conn)
            {
                text="Internet Connection Found";

            }else
            {
                text="No Internet Connection found";

            }

            try{
                Thread.sleep(3000);
            }catch(InterruptedException e)
            {
                e.printStackTrace();
            }

            return text;
        }

        public void onPostExecute(String result){

            pb.setVisibility(View.GONE);
            disp=(TextView)findViewById(R.id.conn);
            disp.setText(result);
            if(result.equals("No Internet Connection found"))
                finish();

        }
        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        public boolean checkconn() {


            ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo!=null && activeNetworkInfo.isConnected();
        }
    }
}
ACTIVITY_MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 >
 <ImageView
 android:id="@+id/logo"
 android:layout_width="150dp"
 android:layout_height="150dp"
 android:layout_centerVertical="true"
 android:src="@mipmap/ic_launcher"
 android:layout_centerHorizontal="true"
 />
 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/logo"
 android:layout_centerHorizontal="true" />
 <TextView
 android:id="@+id/conn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/progressBar1"
 android:textColor="#333333"
 android:layout_centerHorizontal="true"
 android:text=""
 />
</RelativeLayout>



Leave a Comment  
Download Code(80)





All rights reserved, the content is copyrighted to the author.

RELATED POST