您的位置:首页 > 移动开发 > Android开发

Android原生定位

2016-02-19 18:12 357 查看
虽然说现在有很多定位的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);

以上就是关键代码了~不是很清晰,见谅。。只为了自己收集积累知识用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: