• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 0 comments

CHECK THE AVAILABILTY OF INTERNET

SOURCE CODE [main.xml] is

<?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" />

                <Button android:text="Check Network Status"
android:id="@+id/button1"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


SOURCE CODE [NetworkCheck.java] is

package com.NetworkCheck;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class NetworkCheck extends Activity
{

                Button check;

                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                check = (Button) findViewById(R.id.button1);
                               
                                check.setOnClickListener(new OnClickListener()
{

                                                public void onClick(View v)
{
                                                               
                                                                if(isInternetOn())
                                                                {
                                                                                Toast.makeText(getBaseContext(), "Connected",
Toast.LENGTH_SHORT).show();
                                                                }
                                                                else
{
                                                                                Toast.makeText(getBaseContext(), "Not connected",
                                                                                                Toast.LENGTH_SHORT).show();
                                                                }
                                                               
                                                }});

                }

                public final boolean isInternetOn()
{

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

                                if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
                ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
                ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
                ||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
{
                                                return true;
                                }

else if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
                || (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
{
                                                return false;
                                }

                                return false;
                }

}

Note:

Don’t forget to add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in your AndroidManifest.xml.

The OUTPUT will be


https://lh6.googleusercontent.com/-LU5cxbO-Hck/TlyxAFAJf7I/AAAAAAAAAMk/HnmvkNyE4d0/network_check.png




Posted by Jack - - 2 comments

SCREEN ORIENTATION

SOURCE CODE [main.xml] is

<?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" />

                <Button android:text="Set PORTRAIT"
android:id="@+id/portrait"
                                android:layout_width="fill_parent"
android:layout_height="wrap_content">
                </Button>

                <Button android:text="Set LANDSCAPE"
android:id="@+id/landscape"
                                android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>

</LinearLayout>



SOURCE CODE [ScreenOrientationExample.java] is


package com.ScreenOrientationExample;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ScreenOrientationExample extends Activity
{
               
                int o;

                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                Button buttonSetPortrait = (Button) findViewById(R.id.portrait);
                                Button buttonSetLandscape = (Button) findViewById(R.id.landscape);

                                buttonSetPortrait.setOnClickListener(new Button.OnClickListener()
{
                                                public void onClick(View arg0)
{
                                                                o = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                                                                setRequestedOrientation(o);
                                                }
                                });

                                buttonSetLandscape.setOnClickListener(new Button.OnClickListener()
{
                                                public void onClick(View arg0)
{
                                                                o = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                                                                setRequestedOrientation(o);
                                                }
                                });

                }
}

The OUTPUT will be

https://lh6.googleusercontent.com/-hY3JubF0mFE/TkpEhF4TkGI/AAAAAAAAAMY/fudOREETqbA/screen_orientation1.png

https://lh6.googleusercontent.com/-C8DI7uDlkgk/TkpEhMTPHfI/AAAAAAAAAMc/uJZ7AgMbQ7w/screen_orientation2.png