package com.demointernet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;
public class InternetDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = mgr.getActiveNetworkInfo();
if (netInfo != null) {
if (netInfo.isConnected())
{
Toast.makeText(getApplicationContext(), "Connected", 5000).show();
// do something
} else {
AlertDialog.Builder alertbuilder = new AlertDialog.Builder(InternetDemoActivity.this);
alertbuilder.setTitle("Internet");
alertbuilder.setMessage("Internet is not available");
// alertbuilder.setNeutralButton("Ok", );
//alertbuilder.setIcon(R.drawable.warning);
alertbuilder.show();
}
}
else
{
AlertDialog.Builder alertbuilder = new AlertDialog.Builder(
InternetDemoActivity.this);
alertbuilder.setTitle("Internet");
alertbuilder.setMessage("Internet is not available");
// alertbuilder.setNeutralButton("Ok", okClickListener);
alertbuilder.show();
}
}
}
Add ManifistFile two permision like
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
No comments:
Post a Comment