package cn.MyAndroid.Test;import java.io.BufferedReader;import java.io.InputStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;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.JSONObject;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.telephony.TelephonyManager;import android.telephony.gsm.GsmCellLocation;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MyLocation extends Activity {TextView mTextView;Button mButton;TelephonyManager tm;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mTextView = (TextView) findViewById(R.id.TextView01);mButton = (Button) findViewById(R.id.Button01);tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);mButton.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubGsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();int cid = gcl.getCid();int lac = gcl.getLac();int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));try {//组装JSON查询字符串JSONObject holder = new JSONObject();holder.put("version", "1.1.0");holder.put("host", "maps.google.com");// holder.put("address_language", "zh_CN");holder.put("request_address", true);JSONArray array = new JSONArray();JSONObject data = new JSONObject();data.put("cell_id", cid); // 25070data.put("location_area_code", lac);// 4474data.put("mobile_country_code", mcc);// 460data.put("mobile_network_code", mnc);// 0array.put(data);holder.put("cell_towers", array);// 创建连接,发送请求并接受回应DefaultHttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost("http://www.google.com/loc/json");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();}mTextView.setText(sb.toString());} catch (Exception e) {// TODO: handle exception}}});}}
注意要给Activity加入以下权限:<uses-permission android:name="android.permission.PERMISSION_NAME" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
关于基站查询协议的话,请参考这个链接: http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html

更多相关文章

  1. Android(安卓)之gson字符串转java bean
  2. Accessing Resources
  3. Android学习笔记(17)————AutoCompleteTextView与TextWatcher
  4. android 数据库查询记录
  5. Android(安卓)java在窗口画图写字符串
  6. 在Activity中注册广播
  7. Android中十六进制颜色字符串转int的方法
  8. Http 以post方式获取数据
  9. andriod之ContentProvider读取外部存储图片

随机推荐

  1. android默认系统日期、时间、时区更改
  2. android 上面js java数据互传
  3. 【Android】Android开发规范详解
  4. [导入]2010-03-02 传智播客—Android(四)数
  5. 使用 Android(安卓)Studio 检测内存泄漏
  6. Android的Application中onCreate执行多次
  7. Android的消息机制,用Android线程…
  8. 详解Android JS相互调用
  9. Android(安卓)遍历文件夹,搜索指定扩展名
  10. Android 版权介绍