Ich habe das Senden und Empfangen von Broadcasts in meiner Anwendung getestet, aber ich habe ein Problem. Der Dienst startet und ich bekomme den Toast zu sagen, dass die Sendung gesendet wurde, aber onReceive wird nie in der myMapView-Klasse aufgerufen. Ich habe nur den Code, den ich denke, ist relevant unten enthalten... jede Hilfe wäre sehr geschätzt werden. Ich glaube, ich registriere den Empfänger nicht richtig.
Vielen Dank im Voraus.
public class myLocationService extends Service implements LocationListener {
private static final String GEO_LNG = "GEO_LNG";
private static final String GEO_LAT = "GEO_LAT";
private void updateWithNewLocation(Location location){
if (location != null){
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
Intent intent = new Intent(this, myMapView.class);
intent.putExtra(GEO_LNG,geoLng);
intent.putExtra(GEO_LAT, geoLat);
sendBroadcast(intent);
Toast.makeText(myLocationService.this, "Broadcast sent", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(myLocationService.this, "Location = null", Toast.LENGTH_SHORT).show();
}
}
}
public class myMapView extends MapActivity{
private static final String GEO_LNG = "GEO_LNG";
private static final String GEO_LAT = "GEO_LAT";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
IntentFilter filter = new IntentFilter();
filter.addAction(GEO_LONG);
filter.addAction(GEO_LAT);
registerReceiver(locationRec, filter);
Intent i = new Intent(this, myLocationService.class);
startService(i);
}
private BroadcastReceiver locationRec = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
double geoLng = intent.getExtras().getDouble(GEO_LONG);
double geoLat = intent.getExtras().getDouble(GEO_LAT);
Toast.makeText(GeoTrailsMap.this, "Got broadcast", Toast.LENGTH_LONG).show();
}
};
bearbeiten Ich habe meinen Code so geändert, dass er wie folgt aussieht, aber ich habe immer noch kein Glück mit dem Empfang der Sendungen.
public class myLocationService extends Service implements LocationListener {
private static final String GEO_LNG = "GEO_LNG";
private static final String GEO_LAT = "GEO_LAT";
public static final String LOCATION_UPDATE = "LOCATION_UPDATE";
private void updateWithNewLocation(Location location){
if (location != null){
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
Intent intent = new Intent(this, myMapView.class);
intent.putExtra(GEO_LNG,geoLng);
intent.putExtra(GEO_LAT, geoLat);
intent.setAction(LOCATION_UPDATE);
sendBroadcast(intent);
Toast.makeText(myLocationService.this, "Broadcast sent", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(myLocationService.this, "Location = null", Toast.LENGTH_SHORT).show();
}
}
}
public class myMapView extends MapActivity{
private static final String GEO_LNG = "GEO_LNG";
private static final String GEO_LAT = "GEO_LAT";
private static final String LOCATION_UPDATE = myLocationService.LOCATION_UPDATE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
IntentFilter filter = new IntentFilter();
filter.addAction(LOCATION_UPDATE);
registerReceiver(locationRec, filter);
Intent i = new Intent(this, myLocationService.class);
startService(i);
}
private BroadcastReceiver locationRec = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
double geoLng = intent.getExtras().getDouble(GEO_LNG);
double geoLat = intent.getExtras().getDouble(GEO_LAT);
Toast.makeText(GeoTrailsMap.this, "Got broadcast", Toast.LENGTH_LONG).show();
}
};