[android-developers] How do I show the wifi status programatically in Android?

2012-08-23 Thread Jason Hsu


I'm just trying to write a simple Android app that programatically shows 
whether the WiFi is enabled or disabled. I'm unable to get it to print the 
Wifi status. I'm pretty sure that my code is 99% correct, but that's no 
better than being 0% correct.

My code is below:
package com.jasonhsu.wifitest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

private LinearLayout ShowWifiStatus;
TextView TextWifiStatus;

@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShowWifiStatus = new LinearLayout (this);
TextView TextWifiStatus = new TextView (this);
TextWifiStatus.setText(Sorry, your Wifi status program isn't 
working.);
this.registerReceiver(this.WifiStateChangedReceiver, new 
IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
ShowWifiStatus.addView(TextWifiStatus);
setContentView(ShowWifiStatus);
}

private BroadcastReceiver WifiStateChangedReceiver
= new BroadcastReceiver(){

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

int extraWifiState = 
intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE , 
WifiManager.WIFI_STATE_UNKNOWN);

switch(extraWifiState){
case WifiManager.WIFI_STATE_DISABLED:
TextWifiStatus.setText(WIFI STATE DISABLED);
break;
case WifiManager.WIFI_STATE_DISABLING:
TextWifiStatus.setText(WIFI STATE DISABLING);
break;
case WifiManager.WIFI_STATE_ENABLED:
TextWifiStatus.setText(WIFI STATE ENABLED);
break;
case WifiManager.WIFI_STATE_ENABLING:
TextWifiStatus.setText(WIFI STATE ENABLING);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
TextWifiStatus.setText(WIFI STATE UNKNOWN);
break;
}

}};

}

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] How do I show the wifi status programatically in Android?

2012-08-23 Thread Asheesh Arya
please refer this link
http://www.krvarma.com/2010/07/getting-ip-address-of-the-device-in-android/

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en