Android基站定位程序小结

上传人:ji****72 文档编号:37508464 上传时间:2018-04-17 格式:DOC 页数:8 大小:2.03MB
返回 下载 相关 举报
Android基站定位程序小结_第1页
第1页 / 共8页
Android基站定位程序小结_第2页
第2页 / 共8页
Android基站定位程序小结_第3页
第3页 / 共8页
Android基站定位程序小结_第4页
第4页 / 共8页
Android基站定位程序小结_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《Android基站定位程序小结》由会员分享,可在线阅读,更多相关《Android基站定位程序小结(8页珍藏版)》请在金锄头文库上搜索。

1、首先补充 java 的命名规范: 1:包的命名 (全部小写) package ggg.android.demo1;2:类的命名 (单词首字母大写) public class Demo0308Activity extends Activity3:方法(类似的函数的意思)的命名 (首字母小写,随后的单词开头字母大写) public void onCreate(Bundle savedInstanceState)有返回值的方法要求有关键字 return,而返回值为 void 的可以没有. 4:常量的命名 (全部大写 ,常加下划线)* java 是面向对象的,类就可以看做一个对象,类中包含属性和方法,

2、属性是这个对象拥有什么, 方法是这个对象可以做什么. * 问题问题:关键字 import:在程式开头的时候,说明程式中会用到那些类别的简称,理论上可以用* 来代替类后面的详细路径,但是实际编程发现有些包只能省略一层,多省略点层次就会错,暂 时没有明白,先在此处记录一下,日后明白了再补充解释.下面为一个 android 开发的基站位置定位 demo 的程序理解: /自设的公共类Demo0308Activity,通过extends关键字继承Activity这个基类的功能publicpublic classclass Demo0308Activity extendsextends Activity

3、/定义一个公共方法onCreate,该方法无返回值,参数为Bundle型的 savedInstanceState(该名随意)publicpublic voidvoid onCreate(Bundle savedInstanceState) /关键字super表明调用父类(也就是Activity这个基类)中的onCreate方法supersuper.onCreate(savedInstanceState); /是设置当前的Demo0308Activity显示的内容按res/layout/main.xml布局setContentView(R.layout.main); /Button类创建的对象就

4、是一个按钮,关联到main.xml的button1这个id指向的对 象Button btnGetLocation = (Button) findViewById(R.id.button1); /调用setOnClickListener方法设立监听器,监听器类型为点击触发,此处的 OnClickListener应该是匿名内部类btnGetLocation.setOnClickListener(newnew View.OnClickListener() /调用方法onClick,参数arg0名称随意publicpublic voidvoid onClick(View arg0) /调用方法onBt

5、nClickonBtnClick(); );/定义基站信息结构体SCellpublicpublic classclass SCell publicpublic intint MCC; publicpublic intint MNC; publicpublic intint LAC; publicpublic intint CID; /定义经纬度信息结构体SItudepublicpublic classclass SItude publicpublic String latitude; publicpublic String longitude; /定义私有类方法onBtnClickprivat

6、eprivate voidvoid onBtnClick() /类型 变量名 = 新申请ProgressDialog类型的空间,this指的是本对象,当前 编写的类实例化后所产生的对象。ProgressDialog mProgressDialog = newnew ProgressDialog(thisthis); /输出提示信息Now Loading.mProgressDialog.setMessage(“Now Loading.“); /setProgressStyle设置进度条mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SP

7、INNER); /开始展示Dialog的界面mProgressDialog.show(); /try是尝试要执行的程序动作trytry /程序的逻辑顺序SCell cell = getCellInfo(); SItude itude = getItude(cell); String location = getLocation(itude); showResult(cell, location); /dismiss表示结束会话.mProgressDialog.dismiss(); /catch是如果捕获到异常则执行的动作catchcatch (Exception e) /关闭会话,在展示界面的

8、文本位置显示错误的信息mProgressDialog.dismiss(); TextView cellText = (TextView) findViewById(R.id.celltext1);cellText.setText(e.getMessage(); /私有方法getCellInfo.用关键字throws可以抛出异常(throw 是语句抛出一个异常; throws 是方法抛出一个异常)privateprivate SCell getCellInfo() throwsthrows Exception SCell cell = newnew SCell(); /获取系统服务提供的信息赋值

9、给mTelNetTelephonyManager mTelNet = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); /提取信息中的getCellLocation方法返回值赋值给GsmCellLocation类型的location GsmCellLocation location = (GsmCellLocation) mTelNet.getCellLocation(); /如果没有获取成功,则抛错信息为Get Cell failed.ifif (location = nullnull) throwthrow ne

10、wnew Exception(“Get Cell failed“); /获取信息中的getNetworkOperator方法返回值赋值给字符串变量operatorString operator = mTelNet.getNetworkOperator(); intint mcc = Integer.parseInt(operator.substring(0, 3); intint mnc = Integer.parseInt(operator.substring(3); intint cid = location.getCid(); intint lac = location.getLac()

11、;cell.MCC = mcc; cell.MNC = mnc; cell.CID = cid; cell.LAC = lac; /非void的方法,一定要有 return语句.returnreturn cell; /私有方法getItude,返回值类型为SItudeprivateprivate SItude getItude(SCell cell) throwsthrows Exception /新申请SItude类型变量itude的空间SItude itude = newnew SItude(); /新申请HttpClient类型变量client的空间HttpClient client =

12、 newnew DefaultHttpClient(); /调用类HttpPost来准备向google发送HttpPost请求,参数为涉及的url /*无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源。1.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或 HttpPost对象。2.使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST请求,并返 回HttpResponse对象。3.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理。

13、如果使用HttpPost方法提交HTTP POST请求,还需要使用HttpPost类的setEntity 方法设置请求参数。*/ HttpPost post = newnew HttpPost(“http:/ /新申请JavaScript Object Notation类型的变量holderJSONObject holder = newnew JSONObject(); /组装holder的内容,还要求是gsm网络的HTC手机配合holder.put(“version“, “1.1.0“); holder.put(“host“, ““); holder.put(“address_languag

14、e“, “zh_CN“); holder.put(“request_address“, truetrue); holder.put(“radio_type“, “gsm“); holder.put(“carrier“, “HTC“);JSONObject tower = newnew JSONObject(); tower.put(“mobile_country_code“, cell.MCC); tower.put(“mobile_network_code“, cell.MNC); tower.put(“cell_id“, cell.CID); tower.put(“location_are

15、a_code“, cell.LAC); /新申请的JSONArray类型变量towerarrayJSONArray towerarray = newnew JSONArray(); towerarray.put(tower); holder.put(“cell_towers“, towerarray);StringEntity query = newnew StringEntity(holder.toString(); /发送查询请求post.setEntity(query); /获取传送post变量后execute动作的执行结果,此步的详细理解内容待以后 补充.HttpResponse response = client.execute(post); /提取返回信息HttpEntity entity = response.getEntity(); /申请缓存空间来显示返回信息的文本内容.BufferedReader buffReader = newnew BufferedReader(newnew InputStreamReader(entity.getContent();StringBuffer strBuff = newnew StringBuffer(); /初始化提示信息字符串内容为空String result = n

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号