2015-06-04 192 views
0

所以我試圖用locationMenager每5秒更新一個用戶的當前位置,但它不工作。下面的代碼: GPSTracker.java位置Menager - 位置更新

public class GPSTracker extends Service implements LocationListener { 


private final Context context; 

boolean isGPSEnabled = false; 
boolean isNetworkEnabled = false; 
boolean canGetLocation = false; 

Location location; 

double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
private static final long MIN_TIME_BW_UPDATES = 5000; 

protected LocationManager locationManager; 

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

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

     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if(!isGPSEnabled && !isNetworkEnabled) { 

     } else { 
      this.canGetLocation = true; 

      if (isNetworkEnabled) { 

       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

        if (location != null) { 

         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 

      } 

      if(isGPSEnabled) { 
       if(location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        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; 
} 


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

public double getLatitude() { 
    if(location != null) { 
     latitude = location.getLatitude(); 
    } 
    return latitude; 
} 

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

    return longitude; 
} 

public boolean canGetLocation() { 
    return this.canGetLocation; 
} 

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

    alertDialog.setTitle("GPS is settings"); 

    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      context.startActivity(intent); 
     } 
    }); 

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 

    alertDialog.show(); 
} 

@Override 
public void onLocationChanged(Location arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

}

正如你可以看到它被設置爲更新。這是的onCreate()在SearchActivity.java

gps = new GPSTracker(SearchActivity.this); 

    if (gps.canGetLocation()) { 


     ParseUser user2 = ParseUser.getCurrentUser(); 
     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 

     geoPoint = new ParseGeoPoint(latitude, longitude); 
     user2.put("lat_long", geoPoint); 
     textView.setText(latitude+" "+longitude); 
     user2.saveInBackground(); 


    } else { 
     gps.showSettingsAlert(); 
    } 

所以,如果我把上面的代碼中的onClick方法它的工作原理,它更新當前位置。我試圖用TimerTask來實現它,但是結果是(0,0)。那麼有人知道我能在這裏做什麼嗎?因爲最好我想爲該位置設置一個開/關切換,如果可能,請使其在後臺運行,但首先我想通過位置更新解決問題。

回答

0
@Override 
public void onLocationChanged(Location location) { 
    this.location = location; 
    getLatitude(); 
    getLongitude(); 

    ParseUser user2 = ParseUser.getCurrentUser(); 
    ParseGeoPoint geoPoint = new ParseGeoPoint(getLatitude(), getLongitude()); 
    user2.put("lat_long", geoPoint); 
    user2.saveInBackground(); 




} 

好吧所以這是答案,只需要改變onLocationChanged。