J'essaie de faire une application de localisation de localisation sur un téléphone android, ce sera exactement la même chose que la carte Google intégrée dans le téléphone. Il continuera à suivre le téléphone, même si l'application est proche, il sera donc exécuté en arrière-plan des autres applications. Je pense diviser à la fois les classes, l'interface utilisateur (Google Map) et le suivi de localisation, où le suivi de localisation sera toujours exécuté mais la carte google ne s'affichera que lorsque l'utilisateur ouvrira l'application. Ainsi c'est ce que j'ai atteint jusqu'ici.Comment séparer l'auditeur de localisation et Google map
classe MapDisplay pour Google Map
public class MapDisplay extends Activity {
private GoogleMap map;
private LocationService locationService;
private Intent locationServiceIntent;
private Location myLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_display);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.googleMap)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setMyLocationEnabled(true);
locationServiceIntent = new Intent(this, LocationService.class);
x=0;
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
startService(locationServiceIntent);
bindService(locationServiceIntent, locationServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void updateLocation()
{
// for testing purpose, to make sure it will update periodically
Toast.makeText(MapDisplay.this, String.valueOf(myLocation.getLatitude()), Toast.LENGTH_SHORT).show();
}
private ServiceConnection locationServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
locationService = ((LocationService.LocationBinder)service).getService();
myLocation = locationService.getMyLocation();
updateLocation();
}
@Override
public void onServiceDisconnected(ComponentName name) {
locationService = null;
}
};
}
classe LocationService
public class LocationService extends Service implements LocationListener
{
private LocationManager locationManager;
private Location myLocation;
private IBinder locationBinder = new LocationBinder();
public class LocationBinder extends Binder
{
public LocationService getService()
{
return LocationService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return locationBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocation = null;
boolean networkProviderAvailable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean gpsProviderAvailable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(networkProviderAvailable)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
else if(gpsProviderAvailable)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else
{
stopSelf();
}
return super.onStartCommand(intent, flags, startId);
}
public Location getMyLocation()
{
return myLocation;
}
@Override
public void onLocationChanged(Location location) {
myLocation = location;
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
En utilisant du code ci-dessus j'ai connu plantage de l'application en raison de cette ligne
myLocation = locationService.getMyLocation();
String.valueOf(myLocation.getLatitude())
Il ne peut pas obtenir l'emplacement à partir de la classe LocationService.
Alors, mes questions sont les suivantes:
- 1) est-ce une bonne façon de faire la demande?
- 2) comment appeler périodiquement updateLocation() dans MapDisplay lorsqu'il y a de nouvelles mises à jour dans la méthode onLocationChanged?
Je serai très reconnaissant si quelqu'un peut m'aider à ce sujet.
cool, merci je vais jeter un oeil – user3833542