发布于 2017-09-23 06:14:17 | 177 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现GPS位置获取的方法,结合具体实例形式分析了Android针对GPS定位的常见操作技巧,需要的朋友可以参考下

本文实例讲述了Android编程实现GPS位置获取的方法。分享给大家供大家参考,具体如下:


public class GPSInfoService {
  private static GPSInfoService mInstance;
  private LocationManager locationManager;//定位服务
  private GPSInfoService(Context context) {
    // TODO Auto-generated constructor stub
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  }
  public static GPSInfoService getInstance(Context context){
    if(mInstance == null){
      mInstance = new GPSInfoService(context);
    }
    return mInstance;
  }
  //注册定位监听
  public void registenerLocationChangeListener(){
    //得到所有的定位服务
//   List<String> providers = locationManager.getAllProviders();
//   for(String provider:providers){
//     Log.i("i", provider);
//   }
    //查询条件
    Criteria criteria = new Criteria();
    //定位的精准度
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    //海拔信息是否关注
    criteria.setAltitudeRequired(false);
    //对周围的事情是否进行关心
    criteria.setBearingRequired(false);
    //是否支持收费的查询
    criteria.setCostAllowed(true);
    //是否耗电
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    //对速度是否关注
    criteria.setSpeedRequired(false);
    //得到最好的定位方式
    String provider = locationManager.getBestProvider(criteria, true);
    //注册监听
    locationManager.requestLocationUpdates(provider, 60000, 0, getListener());
  }
  //取消监听
  public void unRegisterLocationChangeListener(){
    locationManager.removeUpdates(getListener());
  }
  private MyLocationListener listener;
  //得到定位的监听器
  private MyLocationListener getListener(){
    if(listener == null){
      listener = new MyLocationListener();
    }
    return listener;
  }
  //得到上个地理位置
  public String getLastLocation(){
    return sp.getString("last_location", "");
  }
  private final class MyLocationListener implements LocationListener{
    //位置的改变
    public void onLocationChanged(Location location) {
      // TODO Auto-generated method stub
      double latitude = location.getLatitude();//维度
      double longitude = location.getLongitude();//经度
      String last_location = "jingdu: " + longitude + ",weidu:" + latitude;
      Editor editor = sp.edit();
      editor.putString("last_location", last_location);
      editor.commit();
    }
    //gps卫星有一个没有找到
    public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub
    }
    //某个设置被打开
    public void onProviderEnabled(String provider) {
      // TODO Auto-generated method stub
    }
    //某个设置被关闭
    public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub
    }
  }
}

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务