Skip to content

Instantly share code, notes, and snippets.

@itog
Created May 19, 2010 01:11
Show Gist options
  • Save itog/405815 to your computer and use it in GitHub Desktop.
Save itog/405815 to your computer and use it in GitHub Desktop.
/**
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
*/
package com.itog_lab.sample.wifi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class EnableWifi extends Activity {
static final String TAG = "EnableWifi";
WifiManager wifiManager;
WifiReceiver wifiReceiver;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this.getApplicationContext();
setContentView(R.layout.main);
wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiReceiver = new WifiReceiver();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
}
class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent i) {
NetworkInfo info = i.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info.isConnected()) {
Toast.makeText(context, "connected", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "not connected");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment