package com.lx.util;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
/*
 * 要求是单态的 ,只允许存在一个实例.
 * 获取手机的gps信息
 */
public class GPSInfoService {
    private Context context;
    private LocationManager manager;
    SharedPreferences sp ;
    //1. 私有化构造方法
    private  GPSInfoService(Context context){  
        this.context= context;
        manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
    }
    private static GPSInfoService mGPSService;
     
    public synchronized static GPSInfoService getInstance(Context context){
        if(mGPSService==null)
            mGPSService = new GPSInfoService(context);
        return mGPSService;
    }
     
    public void registerLocationUpdates(){
        //当前你的手机 所支持的定位方式获取出来
        //有多种定位方式 gps network ,基站, passive
        //可以根据定位的条件 ,获取 一个最好的定位方式
        Criteria criteria = new Criteria();
        // 设置定位的精度
        criteria.setAccuracy(Criteria.ACCURACY_COARSE); //获取大体的位置
        criteria.setAltitudeRequired(false); // 海拔信息
        criteria.setCostAllowed(true); //允许产生费用
        criteria.setPowerRequirement(Criteria.POWER_LOW); //低功耗
         
        //获取一个最符合查询条件的位置提供者
        String provider  =manager.getBestProvider(criteria, true);
         
        // 注册 位置改变的监听器
        manager.requestLocationUpdates(provider, 60000, 0, getLinster());
         
    }
     
     
    public void cancleLocationUpdates(){
        manager.removeUpdates(getLinster());
         
    }
    private static MyGPSLinster myGPSLinser;
     
    private MyGPSLinster getLinster(){
        if(myGPSLinser==null)
            myGPSLinser = new MyGPSLinster();
        return myGPSLinser;
    }
     
    /**
     * 获取手机的最后一次位置
     * @return
     */
    public String getLastPosition(){
        return sp.getString("lastlocation", "");
    }
    private class MyGPSLinster implements LocationListener{
 
        // 用户位置改变的时候 的回调方法
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            //location
            //获取到用户的纬度
            double latitude= location.getLatitude();
            double longitude = location.getLongitude();
            String locationstr = "jing du "+ longitude + " weidu  :"+latitude;
            Editor  editor =  sp.edit();
            editor.putString("lastlocation", locationstr);
            editor.commit();
        }
        // 状态改变
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
        //gps ,打开
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
             
        }
        //关闭
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
             
        }
    }
}

android定位GPS定位 代码实现的更多相关文章

  1. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  2. Android中GPS定位的简单应用

    在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...

  3. android 获取GPS定位

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  4. 【Android】GPS定位基本原理浅析

    位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...

  5. Android开发——GPS定位

    1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...

  6. Android之GPS定位详解

    一.LocationManager LocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager.我们可以通过如下代码创建LocationManger ...

  7. Arcgis API for Android之GPS定位

    欢迎大家增加Arcgis API for Android的QQ交流群:337469080 先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的 ...

  8. Arcgis For Android之GPS定位实现

    翻开曾经做的东西,看了看,非常多从逻辑上比較乱,对之做了改动,完毕后实现的效果为: MapActivity源码例如以下: package com.lzugis.map; import java.io. ...

  9. Gps定位和wifi定位和基站定位的比较

    现在手机定位的方式是:Gps定位,wifi定位,基站定位 Gps定位的前提,手机开启Gps定位模块,在室外,定位的精度一般是几米的范围 wifi定位的前提,手机要开启wifi,连不连上wifi热点都可 ...

随机推荐

  1. C# webApi 与 AngularJs 实现增删改Demo 讲解(一)

    公司在使用webAPI+AngularJs+SlcikGrid进行产品开发,自己也是初学Angular,就做了一个Demo,实现增删改功能,希望可以帮助大家. 界面如同所示:  数据库一张单表很简单, ...

  2. java多线程学习-同步之线程通信

    这个示例是网上烂大街的,子线程循环100次,主线程循环50次,但是我试了很多次,而且从网上找了很多示例,其实多运行几次,看输出结果并不正确.不知道是我转牛角尖了,还是怎么了.也没有大神问,好痛苦.现在 ...

  3. 技术英文单词贴--R

    R redirect 重定向,改变方向 reference 参考,提及,引用 register 注册,登记,挂号 render 渲染 represent 代表,象征 route 路线,路由,通道 ro ...

  4. Nginx-->进阶-->原理-->Nginx+php+fastcgi的原理与关系

    一.用户对动态PHP网页访问过程 用户浏览器发起对网页的访问:http://192.168.1.103/index.php 用户和nginx服务器进行三次握手进行TCP连接(忽略包括nginx访问控制 ...

  5. Confluence部署攻略 [转]

    一.软件介绍 AtlassianConfluence(简称Confluence)是一个专业的wiki程序.它是一个知识管理的工具,通过它可以实现团队成员之间的协作和知识共享.Confluence不是一 ...

  6. Jquery点击发送按钮后,按钮文本倒计时

    1.html代码 <input type="number" id="mobileNo" placeholder="请输入手机号" /& ...

  7. oracle 安装注意

    1. 本地安装oracle数据库后,并不代表可以用plsql 连接上了.. 如果安装的是64位的oracle,plsql 是不能直接连接的.. 2. 如果是64位的..需要下载一个oracle 客户端 ...

  8. Struts2中请求参数的接收方式和ModelDriven机制及其运用

    odelDriven 为什么需要ModelDriven 所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar ...

  9. 内存管理运算符new delete与内存管理函数malloc free的区别——已经他们对对象创建的过程。

    (1)内存管理函数与内存管理运算符的区别 内存管理函数有内存分配函数,malloc calloc realloc 以及内存释放函数free. 内存管理运算符有new 和delete. 两种内存管理方式 ...

  10. 10. Software, Software Engineering, water fall (瀑布模型),Code Complete等名词的来源

    ①.Software-软件”一词是20世纪60年代才出现的,软件Software——1958年由贝尔实验室的著名统计学家John Tukey 提出软件与硬件一起构成完整的计算机系统,它们是相互依存,缺 ...