Back to project page android_network_discovery.
The source code is released under:
GNU General Public License
If you think the Android project android_network_discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package info.lamatricexiste.network; /* w w w. j a va 2 s .c o m*/ import info.lamatricexiste.network.Network.NetInfo; import info.lamatricexiste.network.Utils.Prefs; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.SupplicantState; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.preference.PreferenceManager; import android.content.SharedPreferences; import android.util.Log; public abstract class ActivityNet extends Activity { private final String TAG = "NetState"; private ConnectivityManager connMgr; protected final static String EXTRA_WIFI = "wifiDisabled"; protected Context ctxt; protected SharedPreferences prefs = null; protected NetInfo net = null; protected String info_ip_str = ""; protected String info_in_str = ""; protected String info_mo_str = ""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ctxt = getApplicationContext(); Log.i("Context of ActivityNet", ""+ctxt); prefs = PreferenceManager.getDefaultSharedPreferences(ctxt); connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); net = new NetInfo(ctxt); } @Override public void onResume() { super.onResume(); setButtons(true); // Listening for network events IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); registerReceiver(receiver, filter); } @Override public void onPause() { super.onPause(); unregisterReceiver(receiver); } protected abstract void setInfo(); protected abstract void setButtons(boolean disable); protected abstract void cancelTasks(); private BroadcastReceiver receiver = new BroadcastReceiver() { public void onReceive(Context ctxt, Intent intent) { info_ip_str = ""; info_mo_str = ""; Log.i("ActivityNet","Entered BroadcastReceiver in ActivityNet"); // Wifi state String action = intent.getAction(); Log.i("ActivityNet","action = "+action); if (action != null) { if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { int WifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1); switch (WifiState) { case WifiManager.WIFI_STATE_ENABLING: info_in_str = getString(R.string.wifi_enabling); break; case WifiManager.WIFI_STATE_ENABLED: info_in_str = getString(R.string.wifi_enabled); break; case WifiManager.WIFI_STATE_DISABLING: info_in_str = getString(R.string.wifi_disabling); break; case WifiManager.WIFI_STATE_DISABLED: info_in_str = getString(R.string.wifi_disabled); break; default: info_in_str = getString(R.string.wifi_unknown); } } if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) && net.getWifiInfo()) { SupplicantState sstate = net.getSupplicantState(); if (sstate == SupplicantState.SCANNING) { info_in_str = getString(R.string.wifi_scanning); } else if (sstate == SupplicantState.ASSOCIATING) { info_in_str = getString(R.string.wifi_associating, (net.ssid != null ? net.ssid : (net.bssid != null ? net.bssid : net.macAddress))); } else if (sstate == SupplicantState.COMPLETED) { info_in_str = getString(R.string.wifi_dhcp, net.ssid); } } } Log.i("ActivityNet","Checked first two conditions"); // 3G(connected) -> Wifi(connected) // Support Ethernet, with ConnectivityManager.TYPE_ETHER=3 final NetworkInfo ni = connMgr.getActiveNetworkInfo(); Log.i("ActivityNet",""+ni); if (ni != null) { if (ni.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { int type = ni.getType(); Log.i("ActivityNet","Type = "+type); if (type == ConnectivityManager.TYPE_WIFI || type == 1) { // WIFI net.getWifiInfo(); Log.i("ActivityNet","Inside TYPE_WIFI"); if (net.ssid != null) { net.getIp(); info_ip_str = getString(R.string.net_ip, net.ip, net.cidr, net.intf); info_in_str = getString(R.string.net_ssid, net.ssid); info_mo_str = getString(R.string.net_mode, getString( R.string.net_mode_wifi, net.speed, WifiInfo.LINK_SPEED_UNITS)); setButtons(false); Log.i("ActivityNet","Coming out of TYPE_WIFI"); } } else if (type == ConnectivityManager.TYPE_MOBILE) { // 3G if (prefs.getBoolean(Prefs.KEY_MOBILE, Prefs.DEFAULT_MOBILE) || prefs.getString(Prefs.KEY_INTF, Prefs.DEFAULT_INTF) != null) { net.getMobileInfo(); if (net.carrier != null) { net.getIp(); info_ip_str = getString(R.string.net_ip, net.ip, net.cidr, net.intf); info_in_str = getString(R.string.net_carrier, net.carrier); info_mo_str = getString(R.string.net_mode, getString(R.string.net_mode_mobile)); setButtons(false); } } } else if (type == 3) { Log.i(TAG, "Ethernet connectivity detected!"); info_mo_str = getString(R.string.net_mode) + getString(R.string.net_mode_eth); } else { Log.i(TAG, "Connectivity unknown!"); info_mo_str = getString(R.string.net_mode) + getString(R.string.net_mode_unknown); } } else { Log.i("ActivityNet","Running CancelTasks"); cancelTasks(); } } else { Log.i("ActivityNet","Running CancelTasks"); cancelTasks(); } Log.i("ActivityNet","Before setInfo()"); // Always update network info setInfo(); Log.i("ActivityNet","Exiting BroadcastReceiver in ActivityNet"); } }; }