public class DemoActivity extends Activity {
private static final String TAG = "DemoActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onRequestLocation(View view) {
switch (view.getId()){
case R.id.gpsBtn:
Log.d(TAG, "GPS button is clicked");
requestGPSLocation();
break;
case R.id.telBtn:
Log.d(TAG, "CellID button is clicked");
requestTelLocation();
break;
case R.id.wifiBtn:
Log.d(TAG, "WI-FI button is clicked");
requestWIFILocation();
break;
case R.id.netBtn:
Log.d(TAG, "Network button is clicked");
requestNetworkLocation();
break;
}
}
private void requestTelLocation() {
TelephonyManager mTelMan = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
String operator = mTelMan.getNetworkOperator();
String mcc = operator.substring(0, 3);
String mnc = operator.substring(3);
GsmCellLocation location = (GsmCellLocation)
mTelMan.getCellLocation();
int cid = location.getCid();
int lac = location.getLac();
JSONObject tower = new JSONObject();
try {
tower.put("cell_id", cid);
tower.put("location_area_code", lac);
tower.put("mobile_country_code", mcc);
tower.put("mobile_network_code", mnc);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
JSONArray array = new JSONArray();
array.put(tower);
List list =
mTelMan.getNeighboringCellInfo();
Iterator
iter = list.iterator();
NeighboringCellInfo cellInfo;
JSONObject tempTower;
while (iter.hasNext()) {
cellInfo = iter.next();
tempTower = new JSONObject();
try {
tempTower.put("cell_id", cellInfo.getCid());
tempTower.put("location_area_code", cellInfo.getLac());
tempTower.put("mobile_country_code", mcc);
tempTower.put("mobile_network_code", mnc);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
array.put(tempTower);
}
JSONObject object = createJSONObject("cell_towers",
array);
requestLocation(object);
}
private void requestWIFILocation() {
WifiManager wifiMan = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiMan.getConnectionInfo();
String mac = info.getMacAddress();
String ssid = info.getSSID();
JSONObject wifi = new JSONObject();
try {
wifi.put("mac_address", mac);
wifi.put("ssid", ssid);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray array = new JSONArray();
array.put(wifi);
JSONObject object = createJSONObject("wifi_towers",
array);
requestLocation(object);
}
private void requestLocation(JSONObject object) {
Log.d(TAG, "requestLocation: " + object.toString());
HttpClient client = new DefaultHttpClient();
HttpPost post = new
HttpPost("http://www.google.com/loc/json");
try {
StringEntity entity = new
StringEntity(object.toString());
post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(new
InputStreamReader(entity.getContent()));
StringBuffer buffer = new StringBuffer();
String result = br.readLine();
while (result != null) {
buffer.append(result);
result = br.readLine();
}
Log.d(TAG, buffer.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private JSONObject createJSONObject(String arrayName,
JSONArray array) {
JSONObject object = new JSONObject();
try {
object.put("version", "1.1.0");
object.put("host", "maps.google.com");
object.put(arrayName, array);
} catch (JSONException e) {
Log.e(TAG, "call JSONObject's put failed", e);
}
return object;
}
private void requestGPSLocation() {
LocationManager mLocMan = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000 * 60, 100, mLocLis);
}
private void requestNetworkLocation() {
LocationManager mLocMan = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000 * 60, 100, mLocLis);
}
private LocationListener mLocLis = new LocationListener()
{
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
Log.d(TAG, "onStatusChanged, provider = " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled, provider = " + provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled, provider = " +
provider);
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.d(TAG, "latitude: " + latitude + ", longitude: " +
longitude);
}
};
}