-1
public class MainActivity extends ActionBarActivity {
    TextView connectionchecktextbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectionchecktextbox = (TextView) findViewById(R.id.connectionchecktextbox);

        if (Utils.isNetworkAvailable(MainActivity.this)) {

            connectionchecktextbox.setVisibility(View.GONE);
        }

        else {

            connectionchecktextbox
                    .setText("It Seems Internet Connection if off");
        }

    }

}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.popupdisplay.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/connectionchecktextbox"
        android:layout_width="fill_parent"
        android:layout_height="25dip"
        android:layout_alignParentBottom="true"
        android:background="#F40C0C"
        android:gravity="center"
        android:text="It Seems Internert Connection if off"
        android:textAlignment="gravity"
        android:textColor="#ffffff" />

</RelativeLayout>

here is Xml

Using this code i am able to display Text message when device is Internet off and On but it display when we open app : means we have to call always on create to show this i want automatic text message should display when app is On then text message should disappear and when off then automatic that text message should appear please tell me how i to apply this

Departure
  • 264
  • 2
  • 11
  • 1
    Automatic?? BTW why do you want to check whether connection is available or not when your application is not on the foreground? Please add more clarity to your question. – Aniruddha Sep 30 '14 at 03:59
  • Like as i Internet off then it should come popup in Our app it should not restart and then check in On Pause it should display – Departure Sep 30 '14 at 04:06

3 Answers3

1

public class NetworkReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (isConnected == true) {

        // initChatHub();
        Toast.makeText(context, "Connected", 1000).show();


    } else {
        Toast.makeText(context, "Disconnected", 1000).show();
    }
}

Try this simple code and enjoy if any Query please inform me.

Edge
  • 925
  • 5
  • 15
  • 31
0

I think you should use Toast for that.

Heres how to use it.

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
  • check this out : http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts do google.. – Shashank Chavan Sep 30 '14 at 04:08
  • that i did it working But suppose our Internet off now i have On Internet then the popup is not disappear we have to again close app then restart it – Departure Sep 30 '14 at 04:11
0

You can use a BroadcastReceiver to do this. The broadcast receiver inform's you about any connectivity changes:

public class InternetBroadcastReceiver extends BroadcastReceiver 
 public static boolean iAmOnline = false;
{
@Override
public void onReceive(Context context, Intent intent) {

    if (isOnline(context)) 
            {
          iAmOnline = true;
         Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show(); 
}else{
        iAmOnline = false;
}


}
public boolean isOnline(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        }
        return false;
    }
}
} 

And register InternetBroadcastReceiver in your manifest file:

<receiver android:name=".InternetBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

In any place in your app you can now check the internet connection using InternetBroadcastRectiver.iAmOnline. :

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61