Tuesday, 16 April 2013

Checking Internet Connection

Demo:


1)Main Activity:

package com.venky.checkingnetworkconnection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CheckingNetworkConnectionActivity extends Activity {
      /** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            ConnectivityManager cm = (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = cm.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) {

                  startActivity(new Intent(getApplicationContext(),
                              BrowserActivity.class));
            }

            else {

                  Log.e("connection", "not connected");

                  Toast.makeText(
                              getApplicationContext(),
                              "Network is not Connected \n 
                               Please Check your Network Connection",
                              Toast.LENGTH_SHORT).show();

            }
      }
}


2)Second Activity:
 
package com.venky.checkingnetworkconnection;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class BrowserActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.browser);

            WebView webView = (WebView) findViewById(R.id.webView1);

            webView.loadUrl("https://www.google.com");
      }

}


 3)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Checking Network Connection"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15dp"
        android:textStyle="bold" />


</LinearLayout>


4) browser.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


5)Download this Project Click Here
 

No comments:

Post a Comment