package com.HelloGoogleMaps;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import android.location.GpsStatus;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
//import android.location.GpsSatellite;
import android.location.Address;
//import android.util.Log;
import android.provider.Settings;
import android.app.*;
import android.content.*;
import android.app.Activity;
import android.content.Context;
public class HelloGoogleMaps extends Activity {
protected EditText Longitude1 = null;
protected EditText latitude1 = null;
protected EditText address1 = null;
//private GpsStatus.Listener statusListener;
//private GpsStatus gpsStatus;
LocationManager locationManager;
LocationListener locationListener;
String provider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.btn_02);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
Longitude1 = (EditText)findViewById(R.id.Longitude1);
latitude1 = (EditText)findViewById(R.id.latitude1);
String Longitude = Longitude1.getText().toString();
String latitude = latitude1.getText().toString();
intent.putExtra("Longitude1", Longitude);
intent.putExtra("latitude1", latitude);
intent.setClass(HelloGoogleMaps.this,amaps.class);
HelloGoogleMaps.this.startActivity(intent);
}
});
Button button3 = (Button) findViewById(R.id.btn_03);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
openGPSoneSettings();
}
catch(Exception e)
{
new AlertDialog.Builder(HelloGoogleMaps.this)
.setTitle("system error2!")
.setMessage(e.toString())
.show();
}
}
});
Button button2 = (Button) findViewById(R.id.btn_01);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openGPSSettings();
}
});
}
private void openGPSoneSettings()
{
// TODO Auto-generated method stub
TelephonyManager tm;
CdmaCellLocation location;
CellIDInfo info = new CellIDInfo();
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//if (type ==TelephonyManager.NETWORK_TYPE_CDMA) {
location = (CdmaCellLocation) tm.getCellLocation();
if(location == null)
return;
int sid = location.getSystemId();//系统标识 mobileNetworkCode
int bid = location.getBaseStationId();//基站小区号 cellId
int nid = location.getNetworkId();//网络标识 locationAreaCode
ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
info.cellId = bid;
info.locationAreaCode = nid;
info.mobileNetworkCode = String.valueOf(sid);
info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3);
info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
info.radioType = "cdma";
CellID.add(info);
Location loc = callGear(CellID);
if (loc !=null)
Longitude1 = (EditText)findViewById(R.id.Longitude1);
latitude1 = (EditText)findViewById(R.id.latitude1);
Longitude1.append(String.valueOf(loc.getLongitude()));
latitude1.append(String.valueOf(loc.getLatitude()));
//}// end if
}
private Location callGear(ArrayList<CellIDInfo> cellID) {
if (cellID == null)
return null;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.google.com/loc/json");
JSONObject holder = new JSONObject();
try {
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);
holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);
holder.put("radio_type", cellID.get(0).radioType);
holder.put("request_address", true);
if ("460".equals(cellID.get(0).mobileCountryCode))
holder.put("address_language", "zh_CN");
else
holder.put("address_language", "en_US");
JSONObject data,current_data;
JSONArray array = new JSONArray();
current_data = new JSONObject();
current_data.put("cell_id", cellID.get(0).cellId);
current_data.put("location_area_code", cellID.get(0).locationAreaCode);
current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
current_data.put("age", 0);
current_data.put("signal_strength", -60);
current_data.put("timing_advance", 5555);
array.put(current_data);
holder.put("cell_towers", array);
StringEntity se = new StringEntity(holder.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
sb.append(result);
result = br.readLine();
}
data = new JSONObject(sb.toString());
data = (JSONObject) data.get("location");
Location loc = new Location(LocationManager.NETWORK_PROVIDER);
loc.setLatitude((Double) data.get("latitude"));
loc.setLongitude((Double) data.get("longitude"));
loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));
loc.setTime( System.currentTimeMillis());//AppUtil.getUTCTime());
return loc;
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void openGPSSettings()
{
LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.