Android原生定位

虽然说现在有很多定位的lib,但有些不想用第三方的我就写了一个原生定位~用什么保证一直定位呢,我采用的是Service + BroadcastReceiver

首先新建一个Service ,代码贴出如下

package com.mobp2p.mobp2psdk.utils;




import com.mobp2p.mobp2psdk.javabean.Common;


import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


/**
 * @author Liwenjie
 * @date 2016-1-6
 * @version 1.0
 * @desc 定位服务
 * 
 */
public class LocationSvc extends Service implements LocationListener {


private static final String TAG = "LocationSvc";
private LocationManager locationManager;


@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}


@Override
public void onStart(Intent intent, int startId) {
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
//else Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();
}


@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}


@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Get the current position \n" + location + "|" + location.getLatitude() + "|" + location.getLongitude());
//通知Activity
Intent intent = new Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LATITUDE,location.getLatitude() +"");
intent.putExtra(Common.LONGITUDE,location.getLongitude() +"");
sendBroadcast(intent);
// 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。
locationManager.removeUpdates(this);
stopSelf();
}


@Override
public void onProviderDisabled(String provider) {
}


@Override
public void onProviderEnabled(String provider) {
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}


}

写完定位服务后,我们需要用广播接收定位消息,但定位的是经纬度,我们需要转换成地址,那么一并在广播里处理了:

private class LocationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Common.LOCATION_ACTION))
return;
String latitude = intent.getStringExtra(Common.LATITUDE);
String longitude = intent.getStringExtra(Common.LONGITUDE);
SdkPreferences.setLibs(latitude, longitude);
Map<String, Object> callBackMap = new HashMap<String, Object>();
StringBuffer buffer = new StringBuffer();
callBackMap.put("latlng", buffer.append(latitude).append(",")
.append(longitude).toString());
callBackMap.put("sensor", false);
callBackMap.put("language", "zh-CN");
HttpUtils.doGetAsyn(LoadingActivity.this,
"http://maps.google.cn/maps/api/geocode/json", callBackMap,
new CallBack() {
@Override
public void onRequestComplete(String result) {
// TODO Auto-generated method stub
try {
if (result != null) {
JSONObject jsonObject = new JSONObject(
result);
JSONArray array = jsonObject
.optJSONArray("results");
if (array.length() > 0) {
JSONObject addressObject = array
.optJSONObject(0);
SdkPreferences.setAddress(addressObject
.optString("formatted_address"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

接下来需要做的就是在需要用的地方引用了


// 注册广播
IntentFilter filter = new IntentFilter();
filter.addAction(Common.LOCATION_ACTION);
locationBroadcastReceiver = new LocationBroadcastReceiver();
registerReceiver(locationBroadcastReceiver, filter);
// 启动服务
Intent intent = new Intent();
intent.setClass(this, LocationSvc.class);
startService(intent);

以上就是关键代码了~不是很清晰,见谅。。只为了自己收集积累知识用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值