Witam. Mam taki mały problem z gps'em. Napisałem obsługe gpsu aktualizującą się co zadany w usłudze czas i chciałbym wyświetlić pobierane współrzędne w obiekcie TextView w oknie z którego odpalam usługę. Service startuje i chodzi cały czas(wyświetlam toast z informacją że usługa wciąż działa) jednak współrzędne się nie przekazują. Nie wiem już sam co może być źle. Proszę o pomoc. Poniżej kod service i klasy która go uruchamia.
public class navigate extends Service {
private Toast myToast;
public Toast my;
public gps g;
public Timer updatingTimer;
public TimerTask notify = new TimerTask() {
@Override
public void run() {
myToast.setText("Usługa ciągle działa");
myToast.show();
}
};
private Location savedLocation = null;
private LocationManager locationManager;
private LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
@Override
public void onProviderEnabled(String arg0) { }
@Override
public void onProviderDisabled(String arg0) {
showLocation(null);
}
@Override
public void onLocationChanged(Location location) {
showLocation(location);
if(savedLocation == null)
savedLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
};
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
updatingTimer = new Timer();
myToast = Toast.makeText(getApplicationContext(),
"Usługa została uruchomiona",
Toast.LENGTH_SHORT);
myToast.show();
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
savedLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
updatingTimer.scheduleAtFixedRate(notify, 5*1000, 5*1000);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000,
4,
locationListener);
}
@Override
public void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
private void showLocation(Location location) {
String info="";
if(location == null) {
info += "null";
info += "null";
} else {
info += location.getLatitude();
info += location.getLongitude();
}
g.info.setText(info);
my.setText(info); // nawet w usłudze nie wyświetla mi współrzędnych, może problem leży w kodzie gpsu, sam już nie wiem
}
}
public class gps extends Activity {
private Button btnStart;
public TextView info;
private OnClickListener startup = new OnClickListener() {
@Override
public void onClick(View arg0) {
startService(new Intent(gps.this, navigate.class));
}
};
private OnClickListener stopup = new OnClickListener() {
@Override
public void onClick(View arg0) {
stopService(new Intent(gps.this, navigate.class));
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
info = (TextView)findViewById(R.id.position);
info.setText("");
btnStart=(Button) findViewById(R.id.buttonupdate);
btnStart.setOnClickListener(startup);
Button back = (Button) findViewById(R.id.buttonback);
back.setOnClickListener(stopup);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}