如何从gps保存正确的日期和时间

时间:2011-11-30 05:51:36

标签: android gps-time

其实我正在编写代码,以便我可以从该位置保存我的日期和时间 我得到了准确的约会但我的时间是格林威治标准时间+5:30根据我的国家我想保存准确的时间我该怎么做这是我的代码: -

public void fnSetLocManager(){
    try{
        if(lm==null){
            lm=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        }
        try{
            gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
        catch(Exception ex){}

        if(!gps_enabled){
            final Intent poke=new Intent();
             poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
             poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
             poke.setData(Uri.parse("3"));
             sendBroadcast(poke);
        }
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        lm.addGpsStatusListener(gpsStatusListener);
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}
LocationListener locationListener=new LocationListener(){
    public void onLocationChanged(Location location){
        dLat=location.getLatitude();
        dLng=location.getLongitude();

        Date d=new Date();
        _lGpsTime=d.getTime();

        SaveLocation();
    }
    public void onStatusChanged(String provider,int status,Bundle extras){}
    public void onProviderEnabled(String provider){}
    public void onProviderDisabled(String provider){}
};
GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){
    @Override
    public void onGpsStatusChanged(int event){
        if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
            try{
                GpsStatus status=lm.getGpsStatus(null);
                sats=status.getSatellites();
                Iterator satI=sats.iterator();

                int count=0;
                while(satI.hasNext()){
                    GpsSatellite gpssatellite=(GpsSatellite) satI.next();
                    if(gpssatellite.usedInFix()){
                        count++;
                    }
                }
                iSalelliteCount=count;
            }
            catch(Exception ex){}
        }
    }
};

这里我宣布了gps的日期和时间

public void SaveLocation(){
    try{
        lm.removeGpsStatusListener(gpsStatusListener);
        lm.removeUpdates(locationListener);

        if(timer1!=null){
            timer1.cancel();
            timer1=null;
        }

        bIsOnLine=fnIsOnline();
        if(bIsOnLine==false){
            return;
        }

        Date dt=new Date();

        GPS gp=new GPS();

        gp.setMobileNo(sEmpMobile);
        gp.setDOT(new Date(Date.UTC(dt.getYear(),dt.getMonth()+1,dt.getDate(),dt.getHours(),dt.getMinutes(),dt.getSeconds())));
        gp.setLocation("");
        gp.setCoordinateType("GPS");
        gp.setLatitude(BigDecimal.valueOf(dLat));
        gp.setLongitude(BigDecimal.valueOf(dLng));
        gp.setUserID(Integer.parseInt(sUserId));
        gp.setUserName(sUserName);
        gp.setSataliteCount(iSalelliteCount);

0 个答案:

没有答案