1 package com.jouhu.chinamobile; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.UnsupportedEncodingException; 7 import java.util.ArrayList; 8 import java.util.Calendar; 9 import java.util.Locale; 10 11 import org.apache.http.HttpEntity; 12 import org.apache.http.HttpResponse; 13 import org.apache.http.client.ClientProtocolException; 14 import org.apache.http.client.HttpClient; 15 import org.apache.http.client.methods.HttpGet; 16 import org.apache.http.client.methods.HttpPost; 17 import org.apache.http.entity.StringEntity; 18 import org.apache.http.impl.client.DefaultHttpClient; 19 import org.json.JSONArray; 20 import org.json.JSONException; 21 import org.json.JSONObject; 22 23 import android.app.Activity; 24 import android.content.Context; 25 import android.location.Location; 26 import android.location.LocationManager; 27 import android.os.Bundle; 28 import android.telephony.TelephonyManager; 29 import android.telephony.cdma.CdmaCellLocation; 30 import android.telephony.gsm.GsmCellLocation; 31 import android.util.Log; 32 import android.view.View; 33 import android.widget.Button; 34 import android.widget.EditText; 35 36 public class AndroidPositionActivity extends Activity { 37 38 public class CellIDInfo { 39 40 public int cellId; 41 public String mobileCountryCode; 42 public String mobileNetworkCode; 43 public int locationAreaCode; 44 public String radioType; 45 public CellIDInfo(){} 46 } 47 48 private CdmaCellLocation location = null; 49 private Button btnGetInfo = null; 50 /** Called when the activity is first created. */ 51 @Override 52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.main); 55 btnGetInfo = (Button)findViewById(R.id.btnGet); 56 btnGetInfo.setOnClickListener(new View.OnClickListener() { 57 public void onClick(View v) { 58 EditText tv = (EditText)findViewById(R.id.editText1); 59 // TODO Auto-generated method stub 60 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 61 int type = tm.getNetworkType(); 62 //在中国,移动的2G是EGDE,联通的2G为GPRS,电信的2G为CDMA,电信的3G为EVDO 63 //String OperatorName = tm.getNetworkOperatorName(); 64 Location loc = null; 65 ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>(); 66 //中国电信为CTC 67 //NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType 68 //NETWORK_TYPE_CDMA电信2G是CDMA 69 if (type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT) 70 { 71 location = (CdmaCellLocation) tm.getCellLocation(); 72 int cellIDs = location.getBaseStationId(); 73 int networkID = location.getNetworkId(); 74 StringBuilder nsb = new StringBuilder(); 75 nsb.append(location.getSystemId()); 76 CellIDInfo info = new CellIDInfo(); 77 info.cellId = cellIDs; 78 info.locationAreaCode = networkID; //ok 79 info.mobileNetworkCode = nsb.toString(); 80 info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3); 81 info.radioType = "cdma"; 82 CellID.add(info); 83 } 84 //移动2G卡 + CMCC + 2 85 //type = NETWORK_TYPE_EDGE 86 else if(type == TelephonyManager.NETWORK_TYPE_EDGE) 87 { 88 GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); 89 int cellIDs = location.getCid(); 90 int lac = location.getLac(); 91 CellIDInfo info = new CellIDInfo(); 92 info.cellId = cellIDs; 93 info.locationAreaCode = lac; 94 info.mobileNetworkCode = tm.getNetworkOperator().substring(3, 5); 95 info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3); 96 info.radioType = "gsm"; 97 CellID.add(info); 98 } 99 //联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS 100 else if(type == TelephonyManager.NETWORK_TYPE_GPRS) 101 { 102 GsmCellLocation location = (GsmCellLocation)tm.getCellLocation(); 103 int cellIDs = location.getCid(); 104 int lac = location.getLac(); 105 CellIDInfo info = new CellIDInfo(); 106 info.cellId = cellIDs; 107 info.locationAreaCode = lac; 108 //经过测试,获取联通数据以下两行必须去掉,否则会出现错误,错误类型为JSON Parsing Error 109 //info.mobileNetworkCode = tm.getNetworkOperator().substring(3, 5); 110 //info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3); 111 info.radioType = "gsm"; 112 CellID.add(info); 113 } 114 else 115 { 116 tv.setText("Current Not Support This Type."); 117 } 118 119 loc = callGear(CellID); 120 121 if(loc != null) 122 { 123 try { 124 125 StringBuilder sb = new StringBuilder(); 126 String pos = getLocation(loc); 127 sb.append("CellID:"); 128 sb.append(CellID.get(0).cellId); 129 sb.append("+\n"); 130 131 sb.append("home_mobile_country_code:"); 132 sb.append(CellID.get(0).mobileCountryCode); 133 sb.append("++\n"); 134 135 sb.append("mobileNetworkCode:"); 136 sb.append(CellID.get(0).mobileNetworkCode); 137 sb.append("++\n"); 138 139 sb.append("locationAreaCode:"); 140 sb.append(CellID.get(0).locationAreaCode); 141 sb.append("++\n"); 142 sb.append(pos); 143 144 tv.setText(sb.toString()); 145 146 147 } catch (Exception e) { 148 // TODO Auto-generated catch block 149 e.printStackTrace(); 150 } 151 } 152 } 153 }); 154 } 155 156 private Location callGear(ArrayList<CellIDInfo> cellID) { 157 if (cellID == null) return null; 158 DefaultHttpClient client = new DefaultHttpClient(); 159 HttpPost post = new HttpPost( 160 "http://www.google.com/loc/json"); 161 JSONObject holder = new JSONObject(); 162 try { 163 holder.put("version", "1.1.0"); 164 holder.put("host", "maps.google.com"); 165 holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode); 166 holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode); 167 holder.put("radio_type", cellID.get(0).radioType); 168 holder.put("request_address", true); 169 if ("460".equals(cellID.get(0).mobileCountryCode)) 170 holder.put("address_language", "zh_CN"); 171 else 172 holder.put("address_language", "en_US"); 173 JSONObject data,current_data; 174 JSONArray array = new JSONArray(); 175 current_data = new JSONObject(); 176 current_data.put("cell_id", cellID.get(0).cellId); 177 current_data.put("location_area_code", cellID.get(0).locationAreaCode); 178 current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode); 179 current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode); 180 current_data.put("age", 0); 181 array.put(current_data); 182 if (cellID.size() > 2) { 183 for (int i = 1; i < cellID.size(); i++) { 184 data = new JSONObject(); 185 data.put("cell_id", cellID.get(i).cellId); 186 data.put("location_area_code", cellID.get(i).locationAreaCode); 187 data.put("mobile_country_code", cellID.get(i).mobileCountryCode); 188 data.put("mobile_network_code", cellID.get(i).mobileNetworkCode); 189 data.put("age", 0); 190 array.put(data); 191 } 192 } 193 holder.put("cell_towers", array); 194 StringEntity se = new StringEntity(holder.toString()); 195 Log.e("Location send", holder.toString()); 196 post.setEntity(se); 197 HttpResponse resp = client.execute(post); 198 HttpEntity entity = resp.getEntity(); 199 200 BufferedReader br = new BufferedReader( 201 new InputStreamReader(entity.getContent())); 202 StringBuffer sb = new StringBuffer(); 203 String result = br.readLine(); 204 while (result != null) { 205 Log.e("Locaiton receive", result); 206 sb.append(result); 207 result = br.readLine(); 208 } 209 if(sb.length() <= 1) 210 return null; 211 data = new JSONObject(sb.toString()); 212 data = (JSONObject) data.get("location"); 213 214 Location loc = new Location(LocationManager.NETWORK_PROVIDER); 215 loc.setLatitude((Double) data.get("latitude")); 216 loc.setLongitude((Double) data.get("longitude")); 217 loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString())); 218 loc.setTime(GetUTCTime()); 219 return loc; 220 } catch (JSONException e) { 221 return null; 222 } catch (UnsupportedEncodingException e) { 223 e.printStackTrace(); 224 } catch (ClientProtocolException e) { 225 e.printStackTrace(); 226 } catch (IOException e) { 227 e.printStackTrace(); 228 } 229 return null; 230 } 231 232 /** 233 * 获取地理位置 234 * 235 * @throws Exception 236 */ 237 private String getLocation(Location itude) throws Exception { 238 String resultString = ""; 239 240 /** 这里采用get方法,直接将参数加到URL上 */ 241 String urlString = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.getLatitude(), itude.getLongitude()); 242 Log.i("URL", urlString); 243 244 /** 新建HttpClient */ 245 HttpClient client = new DefaultHttpClient(); 246 /** 采用GET方法 */ 247 HttpGet get = new HttpGet(urlString); 248 try { 249 /** 发起GET请求并获得返回数据 */ 250 HttpResponse response = client.execute(get); 251 HttpEntity entity = response.getEntity(); 252 BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent())); 253 StringBuffer strBuff = new StringBuffer(); 254 String result = null; 255 while ((result = buffReader.readLine()) != null) { 256 strBuff.append(result); 257 } 258 resultString = strBuff.toString(); 259 260 /** 解析JSON数据,获得物理地址 */ 261 if (resultString != null && resultString.length() > 0) { 262 JSONObject jsonobject = new JSONObject(resultString); 263 JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString()); 264 resultString = ""; 265 for (int i = 0; i < jsonArray.length(); i++) { 266 resultString = jsonArray.getJSONObject(i).getString("address"); 267 } 268 } 269 } catch (Exception e) { 270 throw new Exception("获取物理位置出现错误:" + e.getMessage()); 271 } finally { 272 get.abort(); 273 client = null; 274 } 275 276 return resultString; 277 } 278 279 public long GetUTCTime() { 280 Calendar cal = Calendar.getInstance(Locale.CHINA); 281 int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); 282 int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); 283 cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); 284 return cal.getTimeInMillis(); 285 } 286 }