婚恋系统源码使用Google提供的LocationManager类。不过只能获取坐标,需要自己多坐标进行处理。
我同样是以服务的形式调用。
//添加定位权限
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
package com.greysun.he.service;
import com.greysun.he.bin.AppSystem;
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.telephony.SmsManager;
import android.util.Log;
public class ListenSeat extends Service{
private LocationManager manager;
private LocationListener locationListener;
@Override
public int onStartCommand(Intent intent,int flags,int startId){
System.out.println("开启位置监听"+AppSystem.getListenerNumber());
manager = (LocationManager)getSystemService(LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//第一次获得设备的位置
updateLocation(location);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override
public void onProviderDisabled(String str) {
System.out.println(str);
}
@Override
public void onProviderEnabled(String str) {
System.out.println(str);
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
System.out.println(arg0+" int: " +arg1+ " Bundle " +arg2);
}
};
//重要函数,监听数据测试
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, locationListener);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
private void updateLocation(Location location) {
String latLng;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLng = "Latitude:" + lat + " Longitude:" + lng;
} else {
latLng = "Can't access your location";
}
latLng = "The location has changed to :" +latLng;
System.out.println("Listen-Seat " + latLng);
}
@Override
public void onDestroy() {
manager.removeUpdates(locationListener);
stopSelf();
System.out.println("关闭位置监听");
}
}
这样,婚恋系统源码就能实现用户定位了。
声明:本文由云豹科技转发自Grey Sun博客,如有侵权请联系作者删除