Search This Blog

Showing posts with label Internet. Show all posts
Showing posts with label Internet. Show all posts

Thursday, March 8, 2012

Checking the network Availability

You can check the network is available or not in your android device.

public boolean isNetworkAvailable() 
{
   ConnectivityManager cm=                        
                 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo networkInfo = cm.getActiveNetworkInfo(); 
   // if no network is available networkInfo will be null, otherwise check if we are connected 

   if (networkInfo != null && networkInfo.isConnected()) 
   { 
             return true; 
    }
 return false; 
}


OR





package com.network.availibility;


import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;


public class NetworkAvailibilityActivity extends Activity {
    /** Called when the activity is first created. */
private boolean isconnected;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        TextView tx = (TextView)findViewById(R.id.ischeking);
        
        
        isconnected = isConnected(NetworkAvailibilityActivity.this);
        
        if(isconnected == true){
        tx.setText("You are connected to the internet");
        }
        else{
        tx.setText("Please connect your phone to internet");
        }
        
        
    }
    public static boolean isConnected(Context context) {
    
    ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);


    NetworkInfo wifiNetwork =
    cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    
    NetworkInfo mobileNetwork =
    cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (wifiNetwork.isAvailable()) {
    return wifiNetwork.isConnectedOrConnecting();
    }


    
    else if (mobileNetwork.isAvailable()) {
    return mobileNetwork.isConnectedOrConnecting();
    }


    
    else if(activeNetwork.isAvailable()) {
    return activeNetwork.isConnectedOrConnecting();
    }
    else{
    return false;
    }
 }
}

You just need to give the permission of the INTERNET and  ACCESS_NETWORK_STATE.

Tuesday, March 6, 2012

Implement Android Browser using simple webview

Here i am posting the creating browser api with simple built in browser functionality in android using WebView.


First of all make an xml file and put the following code here.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<WebView android:id="@+id/wv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</LinearLayout>


Now make an java file and enter the following code here.


public class SimpleWebBrowser extends Activity {
/** Called when the activity is first created. */
           @Override
           public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.main);

                      String myURL = "http://typicaljava.blogspot.com";
                      WebView browser=(WebView)findViewById(R.id.mybrowser);

                      browser.getSettings().setJavaScriptEnabled(true);
                      browser.loadUrl(myURL);
                   }
     }


We just need to add the permission in the menifest file.


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