Android Service获取当前位置GPS+基站Word下载.docx

上传人:b****2 文档编号:5026508 上传时间:2023-05-04 格式:DOCX 页数:35 大小:46.38KB
下载 相关 举报
Android Service获取当前位置GPS+基站Word下载.docx_第1页
第1页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第2页
第2页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第3页
第3页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第4页
第4页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第5页
第5页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第6页
第6页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第7页
第7页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第8页
第8页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第9页
第9页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第10页
第10页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第11页
第11页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第12页
第12页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第13页
第13页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第14页
第14页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第15页
第15页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第16页
第16页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第17页
第17页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第18页
第18页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第19页
第19页 / 共35页
Android Service获取当前位置GPS+基站Word下载.docx_第20页
第20页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Android Service获取当前位置GPS+基站Word下载.docx

《Android Service获取当前位置GPS+基站Word下载.docx》由会员分享,可在线阅读,更多相关《Android Service获取当前位置GPS+基站Word下载.docx(35页珍藏版)》请在冰点文库上搜索。

Android Service获取当前位置GPS+基站Word下载.docx

;

/**移动网络码,共2位,在中国,移动的代码为00和02,联通的代码为01,电信的代码为03,即imsi第4~5位*/

privateStringmobileNetworkCode="

0"

/**地区区域码*/

privateintlocationAreaCode;

/**信号类型[选gsm|cdma|wcdma]*/

privateStringradioType="

"

publicCellInfo(){

}

publicintgetCellId(){

returncellId;

publicvoidsetCellId(intcellId){

this.cellId=cellId;

publicStringgetMobileCountryCode(){

returnmobileCountryCode;

publicvoidsetMobileCountryCode(StringmobileCountryCode){

this.mobileCountryCode=mobileCountryCode;

publicStringgetMobileNetworkCode(){

returnmobileNetworkCode;

publicvoidsetMobileNetworkCode(StringmobileNetworkCode){

this.mobileNetworkCode=mobileNetworkCode;

publicintgetLocationAreaCode(){

returnlocationAreaCode;

publicvoidsetLocationAreaCode(intlocationAreaCode){

this.locationAreaCode=locationAreaCode;

publicStringgetRadioType(){

returnradioType;

publicvoidsetRadioType(StringradioType){

this.radioType=radioType;

2、Gps类–>

Gps封装类,用来获取经纬度

importandroid.content.Context;

importandroid.location.Criteria;

importandroid.location.Location;

importandroid.location.LocationListener;

importandroid.location.LocationManager;

importandroid.os.Bundle;

publicclassGps{

privateLocationlocation=null;

privateLocationManagerlocationManager=null;

privateContextcontext=null;

*初始化

*@paramctx

publicGps(Contextctx){

context=ctx;

locationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

location=locationManager.getLastKnownLocation(getProvider());

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,10,locationListener);

//获取LocationProvider

privateStringgetProvider(){

//构建位置查询条件

Criteriacriteria=newCriteria();

//查询精度:

criteria.setAccuracy(Criteria.ACCURACY_FINE);

//是否查询海拨:

criteria.setAltitudeRequired(false);

//是否查询方位角:

criteria.setBearingRequired(false);

//是否允许付费:

criteria.setCostAllowed(true);

//电量要求:

criteria.setPowerRequirement(Criteria.POWER_LOW);

//返回最合适的符合条件的provider,第2个参数为true说明,如果只有一个provider是有效的,则返回当前provider

returnlocationManager.getBestProvider(criteria,true);

privateLocationListenerlocationListener=newLocationListener(){

//位置发生改变后调用

publicvoidonLocationChanged(Locationl){

if(l!

=null){

location=l;

//provider被用户关闭后调用

publicvoidonProviderDisabled(Stringprovider){

location=null;

//provider被用户开启后调用

publicvoidonProviderEnabled(Stringprovider){

Locationl=locationManager.getLastKnownLocation(provider);

//provider状态变化时调用

publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){

};

publicLocationgetLocation(){

returnlocation;

publicvoidcloseLocation(){

if(locationManager!

if(locationListener!

locationManager.removeUpdates(locationListener);

locationListener=null;

locationManager=null;

3、GpsService服务类

importjava.util.ArrayList;

importandroid.app.Service;

importandroid.content.Intent;

importandroid.os.IBinder;

importandroid.util.Log;

publicclassGpsServiceextendsService{

ArrayList<

CellInfo>

cellIds=null;

privateGpsgps=null;

privatebooleanthreadDisable=false;

privatefinalstaticStringTAG=GpsService.class.getSimpleName();

@Override

publicvoidonCreate(){

super.onCreate();

gps=newGps(GpsService.this);

cellIds=UtilTool.init(GpsService.this);

newThread(newRunnable(){

publicvoidrun(){

while(!

threadDisable){

try{

Thread.sleep(1000);

}catch(InterruptedExceptione){

e.printStackTrace();

if(gps!

=null){//当结束服务时gps为空

//获取经纬度

Locationlocation=gps.getLocation();

//如果gps无法获取经纬度,改用基站定位获取

if(location==null){

Log.v(TAG,"

gpslocationnull"

);

//2.根据基站信息获取经纬度

location=UtilTool.callGear(GpsService.this,cellIds);

}catch(Exceptione){

celllocationnull"

//发送广播

Intentintent=newIntent();

intent.putExtra("

lat"

location==null?

:

location.getLatitude()+"

lon"

location.getLongitude()+"

intent.setAction("

com.ljq.activity.GpsService"

sendBroadcast(intent);

}).start();

publicvoidonDestroy(){

threadDisable=true;

if(cellIds!

=null&

&

cellIds.size()>

0){

cellIds=null;

gps.closeLocation();

gps=null;

super.onDestroy();

publicIBinderonBind(Intentarg0){

returnnull;

4、GpsActivity–>

在界面上实时显示经纬度数据

importandroid.app.Activity;

importandroid.content.BroadcastReceiver;

importandroid.content.IntentFilter;

importandroid.widget.EditText;

importandroid.widget.Toast;

publicclassGpsActivityextendsActivity{

privateDoublehomeLat=26.0673834d;

//宿舍纬度

privateDoublehomeLon=119.3119936d;

//宿舍经度

privateEditTexteditText=null;

privateMyReceiverreceiver=null;

privatefinalstaticStringTAG=GpsActivity.class.getSimpleName();

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

editText=(EditText)findViewById(R.id.editText);

//判断GPS是否可用

Log.i(TAG,UtilTool.isGpsEnabled((LocationManager)getSystemService(Context.LOCATION_SERVICE))+"

if(!

UtilTool.isGpsEnabled((LocationManager)getSystemService(Context.LOCATION_SERVICE))){

Toast.makeText(this,"

GSP当前已禁用,请在您的系统设置屏幕启动。

Toast.LENGTH_LONG).show();

IntentcallGPSSettingIntent=newIntent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);

startActivity(callGPSSettingIntent);

return;

//启动服务

startService(newIntent(this,GpsService.class));

//注册广播

receiver=newMyReceiver();

IntentFilterfilter=newIntentFilter();

filter.addAction("

registerReceiver(receiver,filter);

//获取广播数据

privateclassMyReceiverextendsBroadcastReceiver{

publicvoidonReceive(Contextcontext,Intentintent){

Bundlebundle=intent.getExtras();

Stringlon=bundle.getString("

 

Stringlat=bundle.getString("

if(lon!

!

.equals(lon)&

lat!

.equals(lat)){

doubledistance=getDistance(Double.parseDouble(lat),

Double.parseDouble(lon),homeLat,homeLon);

editText.setText("

目前经纬度\n经度:

+lon+"

\n纬度:

+lat+"

\n离宿舍距离:

+java.lang.Math.abs(distance));

}else{

+lat);

protectedvoidonDestroy(){

//注销服务

unregisterReceiver(receiver);

//结束服务,如果想让服务一直运行就注销此句

stopService(newIntent(this,GpsService.class));

*把经纬度换算成距离

*@paramlat1开始纬度

*@paramlon1开始经度

*@paramlat2结束纬度

*@paramlon2结束经度

*@return

privatedoublegetDistance(doublelat1,doublelon1,doublelat2,doublelon2){

float[]results=newfloat[1];

Location.distanceBetween(lat1,lon1,lat2,lon2,results);

returnresults[0];

5、UtilTool–>

工具类

importjava.io.ByteArrayOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.io.UnsupportedEncodingException;

import.HttpURLConnection;

import.MalformedURLException;

import.ProtocolException;

import.URL;

importjava.util.Calendar;

importjava.util.List;

importjava.util.Locale;

importorg.apache.http.client.ClientProtocolException;

importorg.json.JSONException;

importorg.json.JSONObject;

importandroid.telephony.NeighboringCellInfo;

importandroid.telephony.TelephonyManager;

importandroid.telephony.cdma.CdmaCellLocation;

importandroid.telephony.gsm.GsmCellLocation;

publicclassUtilTool{

publicstaticbooleanisGpsEnabled(LocationManagerlocationManager){

booleanisOpenGPS=locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);

booleanisOpenNetwork=locationManager.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER);

if(isOpenGPS||isOpenNetwork){

returntrue;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2