views:

86

answers:

1

hi everybody, im trying to make an android application that whenever a user conncects to a wifi, application sends an http request to my server, later on by using push notification server sends some msgs to the user. but in here i have a problems: how to make this application running in background and also it understand if phone is connected to a wifi, means even if applcation is closed it still be aware and whenever the phone is connected to a wifi it sends the http request. Thanks

+4  A: 
private void registerForWifiBroadcasts() {
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  mContext.registerReceiver(mReceiver, intentFilter);
}

@Override
public void onReceive(Context context, Intent intent) {
  final String action = intent.getAction();
  if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
    handleNetworkStateChanged(
      (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
  } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
    handleSupplicantConnectionChanged(
    intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false));
  } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
    handleWifiStateChanged(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
      WifiManager.WIFI_STATE_UNKNOWN));
  }
}

And: Make it a Service

Loxley