分享Android CellLocation源码,基于Rexsee对象的基站定位功能

来源:互联网 发布:淘宝异地客服 编辑:程序博客网 时间:2024/05/17 22:40

[代码] 代码段示例

view source
print?
01function query(){
02  var loction = eval('('+rexseeCellLocation.getLastKnownLocation()+')');
03  var type = location.type.toLowerCase();
04  var mcc = parseInt(location.operator.substring(0,3));
05  var mnc = (type=='gsm')?parseInt(location.operator.substring(3)):location.systemId;
06  var cid= (type=='gsm')?location.cid:location.baseStationId;
07  var lac= (type=='gsm')?location.lac:location.networkId;
08  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}]}";
09 
10alert(rexseeAjax.syncSubmit('http://www.google.com/loc/json',postData,'utf-8'));
11}

[图片] 1318156045.jpg

[代码] RexseeCellLocation对象

001/*
002* Copyright (C) 2011 The Rexsee Open Source Project
003*
004* Licensed under the Rexsee License, Version 1.0 (the "License");
005* you may not use this file except in compliance with the License.
006* You may obtain a copy of the License at
007*
008*      http://www.rexsee.com/CN/legal/license.html
009*
010* Unless required by applicable law or agreed to in writing, software
011* distributed under the License is distributed on an "AS IS" BASIS,
012* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013* See the License for the specific language governing permissions and
014* limitations under the License.
015*/
016  
017packagerexsee.location; 
018  
019importrexsee.core.browser.JavascriptInterface; 
020importrexsee.core.browser.RexseeBrowser; 
021importandroid.content.Context; 
022importandroid.telephony.CellLocation; 
023importandroid.telephony.PhoneStateListener; 
024importandroid.telephony.TelephonyManager; 
025importandroid.telephony.cdma.CdmaCellLocation; 
026importandroid.telephony.gsm.GsmCellLocation; 
027  
028public class RexseeCellLocation implements JavascriptInterface { 
029  
030       private static final String INTERFACE_NAME ="CellLocation"
031       @Override 
032       public String getInterfaceName() { 
033               return mBrowser.application.resources.prefix + INTERFACE_NAME; 
034       
035       @Override 
036       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) { 
037               returnthis
038       
039       @Override 
040       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) { 
041               return newRexseeCellLocation(childBrowser); 
042       
043  
044       public static final String EVENT_ONCELLLOCATIONCHANGED ="onCellLocationChanged"
045  
046       public final Context mContext; 
047       private final RexseeBrowser mBrowser; 
048       private final intmPhoneType; 
049       private PhoneStateListener mListener =null
050       private CellLocation mLocation =null
051  
052       public RexseeCellLocation(RexseeBrowser browser) { 
053               mContext = browser.getContext(); 
054               mBrowser = browser; 
055               browser.eventList.add(EVENT_ONCELLLOCATIONCHANGED); 
056               TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 
057               mPhoneType = tm.getPhoneType(); 
058       
059  
060       //JavaScript Interface 
061       public boolean isEnabled() { 
062               return mListener !=null
063       
064       public boolean enable() { 
065               try
066                       TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 
067                       mListener = new PhoneStateListener() { 
068                               @Override 
069                               public void onCellLocationChanged(CellLocation location) { 
070                                       mLocation = location; 
071                                       mBrowser.eventList.run(EVENT_ONCELLLOCATIONCHANGED); 
072                               
073                       }; 
074                       tm.listen(mListener, PhoneStateListener.LISTEN_CELL_LOCATION); 
075                       returntrue
076               catch (Exception e) { 
077                       mBrowser.exception(getInterfaceName(), e); 
078                       returnfalse
079               
080       
081       public boolean disable() { 
082               if (mListener == nullreturntrue
083               try
084                       TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 
085                       tm.listen(mListener, PhoneStateListener.LISTEN_NONE); 
086                       mListener =null
087                       returntrue
088               catch (Exception e) { 
089                       mBrowser.exception(getInterfaceName(), e); 
090                       returnfalse
091               
092       
093       public String getLastKnownLocation() { 
094               if (mLocation == nullreturn "{}"
095               TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 
096               String rtn =""
097               rtn += "\"operator\":\"" + tm.getNetworkOperator() +"\""
098               rtn += ",\"operatorName\":\"" + tm.getNetworkOperatorName() +"\""
099               if (mPhoneType == TelephonyManager.PHONE_TYPE_GSM) { 
100                       GsmCellLocation gsm = (GsmCellLocation) mLocation; 
101                       rtn +=",\"type\":\"GSM\""
102                       rtn += ",\"cid\":" + gsm.getCid(); 
103                       rtn += ",\"lac\":" + gsm.getLac(); 
104               else if (mPhoneType == TelephonyManager.PHONE_TYPE_CDMA) { 
105                       CdmaCellLocation cdma = (CdmaCellLocation) mLocation; 
106                       rtn +=",\"type\":\"CDMA\""
107                       rtn += ",\"baseStationId\":" + cdma.getBaseStationId(); 
108                       rtn += ",\"baseStationLatitude\":" + cdma.getBaseStationLatitude(); 
109                       rtn += ",\"baseStationLongitude\":" + cdma.getBaseStationLongitude(); 
110                       rtn += ",\"networkId\":" + cdma.getNetworkId(); 
111                       rtn += ",\"systemId\":" + cdma.getSystemId(); 
112               
113               return "{" + rtn +"}"
114       
115  
116    }
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星s8应用闪退怎么办 淘宝购物商家不发货怎么办 淘宝商家迟迟不发货怎么办 买家地址写错了怎么办 买家写错快递地址怎么办 淘宝退款选择服务类型出不来怎么办 网购东西发错了怎么办 拒签商家不退款怎么办 红米手机无响应怎么办 淘宝网登录密码忘记了怎么办 淘宝网密码忘记了怎么办 京东抢购不发货怎么办 微商不想做了怎么办 减肥过程中饿了怎么办 滴滴车龄超过6年怎么办 网络公选课没过怎么办 大学网络课挂了怎么办 淘宝虚拟订单买家恶意退款怎么办 淘宝卖家虚拟发货怎么办 淘宝买虚拟产品被骗了怎么办 哈尔滨暖气低于十八度怎么办 淘客店铺没人买怎么办 淘宝商家不给退货怎么办 淘宝卖家拒绝退款申请怎么办 运费险赔付少了怎么办 买了运费险退货怎么办 卖家运费险退货怎么办 京东生鲜有坏的怎么办 与上级意见不一致时你将怎么办 物金所倒闭投资怎么办 电商平台欺骗客户怎么办 pdf电脑打开是乱码怎么办 excel表格打开是乱码怎么办 win10安装软件出现乱码怎么办 华为手机速度越来越慢怎么办 oppo手机速度越来越慢怎么办 安卓手机速度越来越慢怎么办 青桔单车忘了锁怎么办 华为手机反应太慢了怎么办 魅族关机键失灵怎么办 oppa79手机开不开机怎么办