2013-12-02 5 views
0

Я следую учебному курсу, который нажимает на кнопку, которую он вызывает класс, который приносит всю информацию GPS lat/long, дело в том, что мне нужно, чтобы это было автоматически, и каждое определенное время и смещение устройства. Он должен был отображать новые значения на экране, но он отображается только в первый раз. Это то, что у меня есть:Как я могу обновить GPS на экране

public class GPS extends Service implements LocationListener{ 

private final Context mContext; 

//flag for GPS status 
boolean isGPSEnable = false; 

// Flag for network status 
boolean isNetworkEnable = false; 

// flag for GPS status 
boolean canGetLocation = false; 

Location location; //location 
double latitude;  //latitude 
double longitude; //longitude 

// the minimum distances to change Updates in meters 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // Minimun distance 10 meters 

// The minimum time between updates in millisenconds 
private static final long MIN_TIME_BTW_UPDATES = 1000 * 60 * 1; // 1 MINUTE 

// Declaring a Location Manager 
protected LocationManager locationManager; 

public GPS(Context context){ 
    this.mContext = context; 
    getLocation(); 
} 

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(Context.LOCATION_SERVICE); 

     //getting GPS status 
    isGPSEnable = locationManager 
      .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     //getting network status 
     isNetworkEnable = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if(!isGPSEnable && !isNetworkEnable){ 
      // no network provider is enable 
     } 
     else { 
      this.canGetLocation = true; 
      if(isNetworkEnable){ 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BTW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this); 
       Log.d("Network", "Network"); 
       if(locationManager != null){ 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if(location != null){ 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      // if GPS Enable get lat/long using GPS Services 
      if(isGPSEnable){ 
       if(location == null){ 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BTW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS ENABLE", "GPS Enabled"); 
        if(locationManager != null){ 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null){ 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 

    return location; 
} 

/** 
* Stop using GPS listener 
* Calling this function will stop using GPS in your app 
*/ 

public void stopUsingGPS(){ 
    if (locationManager != null){ 
     locationManager.removeUpdates((LocationListener) GPS.this); 
    } 
} 

/** 
* function to get latitude 
*/ 
public double getLatitude(){ 
     if(location != null){ 
      latitude = location.getLatitude(); 
     } 
    //return latitude 
    return latitude; 
} 

public double getLongitude(){ 
    if(location != null){ 
     longitude = location.getLongitude(); 
    } 
    //return longitude 
    return longitude; 
} 

/** 
* Function to check GPS/wifi enable 
* @return boolean 
*/ 
public boolean canGetLocation(){ 
    return this.canGetLocation; 
} 

/** 
* Function to show settings alert dialog 
* On pressing Settings button will launch Settings Options 
*/ 

public void showSettingsAlert(){ 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

    // Setting Dialog Title 
    alertDialog.setTitle(mContext.getString(R.string.AlertDialog_Tittle)); 

    // Setting Dialog Message 
    alertDialog.setMessage(mContext.getString(R.string.dialog_message)); 

    // On pressing Setting button 
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     } 
    }); 

    // On pressing cancel button 
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
} 

@Override 
public void onLocationChanged(Location location){ 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
} 

@Override 
public void onProviderEnabled(String provider) { 
} 

@Override 
public void onProviderDisabled(String provider) { 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

}

на основной деятельности

public class GPSActivity extends ActionBarActivity { 

private EditText edTLatitud; 
private EditText edTLongitud; 
private EditText edTCompass; 
private EditText edTDirecc; 
private SensorManager sensorManager; 
private Sensor compassSensor; 

// GPS class 
GPS gps; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gpsactivity); 

    edTCompass = (EditText)this.findViewById(R.id.edTxtBrujula); 
    edTDirecc = (EditText)this.findViewById(R.id.edTxtBrujdireccion); 
    edTLatitud = (EditText)this.findViewById(R.id.edTxtLatitud); 
    edTLongitud = (EditText)this.findViewById(R.id.edTxtLongitud); 

    gps = new GPS(GPSActivity.this); 

    // Check if GPS is enable 
    if(gps.canGetLocation()){ 

     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 

     // Print on the screen the coordinates 
     UpdateGPSonScreen(latitude, longitude); 
    } 
    else { 
     // can't get location 
     // GPS or network is not enable 
     // Ask user to enable GPS/network in settings 
     gps.showSettingsAlert(); 
    } 
} 

спасибо в совет

ответ

0

Вы звоните UpdateGPSonScreen только один раз. Если вам нужны постоянные обновления, вы будете называть этот метод каждый раз в пределах onLocationChanged(Location location), но для того, чтобы вы это сделали, вам нужно будет немного реорганизовать.

+0

спасибо за ответ @ J.Romero, любые идеи как? – SamCurve

Смежные вопросы