import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class CheckNetStatus extends Activity {
private static final String TAG = "WEBSTREAM";
private static final String URL ="google.com";
private static TextView tv;
public Bundle netstat;
private ProgressDialog progDialog;
private int maxBarValue = 200;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checknetstatus);
tv = (TextView) findViewById(R.id.TextView01);
new BackgroundLoad().execute(URL);
Toast.makeText(this, "Checking Network Configuration",
Toast.LENGTH_LONG).show();
}
public Bundle networkStatus(String serverURL){
Bundle netStatus = new Bundle();
Boolean wifiAvailable = null;
Boolean wifiConnected = null;
Boolean phoneAvailable = null;
Boolean phoneConnected = null;
String ipNumber = null;
int ipInteger = 0;
NetworkInfo netInfo = null;
InetAddress inetAddress = null;
ConnectivityManager conman = (ConnectivityManager) getSystemService
(
Context.CONNECTIVITY_SERVICE);
netInfo = conman.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
wifiAvailable = netInfo.isAvailable();
wifiConnected = netInfo.isConnected();
netInfo = conman.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
phoneAvailable = netInfo.isAvailable();
phoneConnected = netInfo.isConnected();
try {
inetAddress = InetAddress.getByName(serverURL);
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipNumber = inetAddress.getHostAddress();
ipInteger = lookupHost(serverURL);
netStatus.putBoolean("wifiAvailable", wifiAvailable);
netStatus.putBoolean("wifiConnected", wifiConnected);
netStatus.putBoolean("phoneAvailable", phoneAvailable);
netStatus.putBoolean("phoneConnected", phoneConnected);
netStatus.putString("ipNumber", ipNumber);
netStatus.putInt("ipInteger", ipInteger);
return netStatus;
}
public static int lookupHost(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
Log.i(TAG, "inetAddress="+inetAddress);
String ipNumber = inetAddress.getHostAddress();
Log.i(TAG, "ipNumber="+ipNumber);
try {
Log.i(TAG, "isReachable="+inetAddress.isReachable(5000));
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnknownHostException e) {
return -1;
}
byte [] addBytes;
int add;
Log.i(TAG, "***** inetAddress ="+inetAddress);
addBytes = inetAddress.getAddress();
for(int i=0; i<addBytes.length; i++){
Log.i(TAG, " Byte "+i+"="+addBytes[i]);
}
add = ((addBytes[0] & 0xff) << 24)
| ((addBytes[1] & 0xff) << 16)
| ((addBytes[2] & 0xff) << 8)
| (addBytes[3] & 0xff);
Log.i(TAG, "IPinteger: "+add);
return add;
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0: // Spinner
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Loading...");
return progDialog;
case 1: // Horizontal
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progDialog.setMax(maxBarValue);
progDialog.setMessage("Loading...:");
return progDialog;
default:
return null;
}
}
private class BackgroundLoad extends AsyncTask <String, String,
Bundle> {
@Override
protected Bundle doInBackground(String... url) {
Log.i(TAG, "---doInBackground---");
publishProgress("\n\nStarting background thread\n");
return networkStatus(url[0]) ;
}
protected void onPreExecute () {
showDialog(0);
Log.i(TAG, "\n---onPreExecute---");
}
protected void onProgressUpdate(String... progress) {
Log.i(TAG, "---onProgressUpdate---");
tv.append(progress[0]);
}
@Override
protected void onPostExecute(Bundle data){
dismissDialog(0);
Log.i(TAG, "---onPostExecute---");
netstat = data;
String net1 = "phone available =
"+netstat.getBoolean("phoneAvailable");
String net2 = "phone connected =
"+netstat.getBoolean("phoneConnected");
String net3 = "wifi available =
"+netstat.getBoolean("wifiAvailable");
String net4 = "wifi connected =
"+netstat.getBoolean("wifiConnected");
String net5 = "IP = "+netstat.getString("ipNumber");
String net6 = "IPinteger = "+netstat.getInt("ipInteger");
String netString =
net1+"\n"+net2+"\n"+net3+"\n"+net4+"\n"+net5+"\n"+net6;
Log.i(TAG, netString);
tv.append("\n"+netString);
}
}
}
xml file-----------------
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="5sp"
android:id="@+id/TextView01" android:text="@string/netstatus" />
</LinearLayout>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en