分享Android CellLocation源码,基于Rexsee对象的基站定位功能
2014-03-21 15:17
399 查看
转自 :http://www.oschina.net/code/snippet_194999_7244#11570
使用Rexsee的基站定位(RexseeCellLocation对象)。
需注意:基站信息是来自运营商的,仿真器只能模拟网络延迟(-netdelay)、网速(-netspeed)、以及一些电话相关的操作,gsm<call|accept|busy|cancel|data|hold|list|voice|status>。还不能模拟信号。
使用Rexsee的基站定位(RexseeCellLocation对象)。
需注意:基站信息是来自运营商的,仿真器只能模拟网络延迟(-netdelay)、网速(-netspeed)、以及一些电话相关的操作,gsm<call|accept|busy|cancel|data|hold|list|voice|status>。还不能模拟信号。
1. [代码]代码段示例
function query(){ var loction = eval('('+rexseeCellLocation.getLastKnownLocation()+')'); var type = location.type.toLowerCase(); var mcc = parseInt(location.operator.substring(0,3)); var mnc = (type=='gsm')?parseInt(location.operator.substring(3)):location.systemId; var cid= (type=='gsm')?location.cid:location.baseStationId; var lac= (type=='gsm')?location.lac:location.networkId; var postData="{\version\":\"1.1.0\",\"host\":\maps.google.com\",\"access_token\";\"2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe\", \"home_mobile_country_code\":"+mcc+",\"home_mobile_network_code\":"+mnc+",\"address_language\";\"zh_CN\",\"radio_type\"; \""+type+"\",\"request_address\":true,\"cell_towers\":[{\"cell_id\":+cid+",\"location_area_code\":+lac+",\"mobile_aountry_code\": "+mcc+",\"mobile_network_code\":"+mnc+",\"timing_advance\":5555}]}"; alert(rexseeAjax.syncSubmit('http://www.google.com/loc/json',postData,'utf-8')); }
2. [图片] 1318156045.jpg
/* * Copyright (C) 2011 The Rexsee Open Source Project * * Licensed under the Rexsee License, Version 1.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.rexsee.com/CN/legal/license.html * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package rexsee.location; import rexsee.core.browser.JavascriptInterface; import rexsee.core.browser.RexseeBrowser; import android.content.Context; import android.telephony.CellLocation; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.telephony.cdma.CdmaCellLocation; import android.telephony.gsm.GsmCellLocation; public class RexseeCellLocation implements JavascriptInterface { private static final String INTERFACE_NAME = "CellLocation"; @Override public String getInterfaceName() { return mBrowser.application.resources.prefix + INTERFACE_NAME; } @Override public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) { return this; } @Override public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) { return new RexseeCellLocation(childBrowser); } public static final String EVENT_ONCELLLOCATIONCHANGED = "onCellLocationChanged"; public final Context mContext; private final RexseeBrowser mBrowser; private final int mPhoneType; private PhoneStateListener mListener = null; private CellLocation mLocation = null; public RexseeCellLocation(RexseeBrowser browser) { mContext = browser.getContext(); mBrowser = browser; browser.eventList.add(EVENT_ONCELLLOCATIONCHANGED); TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); mPhoneType = tm.getPhoneType(); } //JavaScript Interface public boolean isEnabled() { return mListener != null; } public boolean enable() { try { TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); mListener = new PhoneStateListener() { @Override public void onCellLocationChanged(CellLocation location) { mLocation = location; mBrowser.eventList.run(EVENT_ONCELLLOCATIONCHANGED); } }; tm.listen(mListener, PhoneStateListener.LISTEN_CELL_LOCATION); return true; } catch (Exception e) { mBrowser.exception(getInterfaceName(), e); return false; } } public boolean disable() { if (mListener == null) return true; try { TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); tm.listen(mListener, PhoneStateListener.LISTEN_NONE); mListener = null; return true; } catch (Exception e) { mBrowser.exception(getInterfaceName(), e); return false; } } public String getLastKnownLocation() { if (mLocation == null) return "{}"; TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); String rtn = ""; rtn += "\"operator\":\"" + tm.getNetworkOperator() + "\""; rtn += ",\"operatorName\":\"" + tm.getNetworkOperatorName() + "\""; if (mPhoneType == TelephonyManager.PHONE_TYPE_GSM) { GsmCellLocation gsm = (GsmCellLocation) mLocation; rtn += ",\"type\":\"GSM\""; rtn += ",\"cid\":" + gsm.getCid(); rtn += ",\"lac\":" + gsm.getLac(); } else if (mPhoneType == TelephonyManager.PHONE_TYPE_CDMA) { CdmaCellLocation cdma = (CdmaCellLocation) mLocation; rtn += ",\"type\":\"CDMA\""; rtn += ",\"baseStationId\":" + cdma.getBaseStationId(); rtn += ",\"baseStationLatitude\":" + cdma.getBaseStationLatitude(); rtn += ",\"baseStationLongitude\":" + cdma.getBaseStationLongitude(); rtn += ",\"networkId\":" + cdma.getNetworkId(); rtn += ",\"systemId\":" + cdma.getSystemId(); } return "{" + rtn + "}"; } }
相关文章推荐
- 分享Android CellLocation源码,基于Rexsee对象的基站定位功能
- 分享Android CellLocation源码,基于Rexsee对象的基站定位功能
- android 基于GeolocationAPI的基站定位
- Android基于百度定位SDK v4.1(LBS)实现定位功能
- android开发通过基站手机网络实现定位功能(Google实现)
- Android基于百度API的定位功能
- 基于android系统实现的简单定位功能
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- 基于Android源码开发的来电接通振动功能实现
- android定位之基站的定位(含GSM与cdma实现源码)
- Android开发之发送邮件功能的实现(源码分享)
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- 基于Android的sina微博分享功能
- 分享一个可快速开发分享功能Android开源组件源码
- Android基于各官方API实现分享功能(不使用第三方集成工具)
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android开发之Navigationdrawer导航抽屉功能的实现(源码分享)