2010-11-04 5 views
3

J'ai testé l'envoi et la réception de diffusions dans mon application, mais j'ai un problème. Le service démarre et je reçois le Toast pour dire que la diffusion a été envoyée mais onReceive n'est jamais appelé dans la classe myMapView. J'ai seulement inclus le code que je pense est pertinent ci-dessous ... toute aide serait très appréciée. Je ne pense pas que je suis en train d'enregistrer le récepteur correctement.Récepteur de diffusion onReceive() jamais appelé - Android

Merci d'avance.

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(); 


     } 
    }; 

Modifier J'ai modifié mon code pour ressembler à ceci, mais je ne suis toujours pas avoir de chance avec la réception des émissions.

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(); 


    } 
}; 

Répondre

2

L'intention qui est mise à feu ne dispose pas d'un jeu d'action. Lorsque vous appelez « addAction » dans le IntentFilter ici.

IntentFilter filter = new IntentFilter(); 
    filter.addAction(GEO_LNG); 
    filter.addAction(GEO_LAT); 
    registerReceiver(locationRec, filter); 

Vous ajoutez des actions que le récepteur écoutera (dans ce cas, le récepteur écoutera les GEO_LNG et GEO_LAT

Au service où vous tirez l'intention, l'intention doit contenir que des mesures pour que le récepteur pour le repérer à décider lequel vous voulez envoyer, puis modifier le code d'intention mise à feu pour ressembler à ceci:.

 Intent intent = new Intent(this, myMapView.class); 
     // Here you're using GEO_LNG as both the Intent action, and a label 
     // for data being passed over. Might want to change that for clarity? 
     intent.putExtra(GEO_LNG, geoLng); 
     intent.putExtra(GEO_LAT, geoLat); 
     intent.setAction(GEO_LNG); 
     ... 
+0

Salut merci pour cela! Je pense que je suis presque là, s'il vous plaît pourriez-vous jeter un oeil à mon édition et voyez ce que vous en pensez? Je n'obtiens toujours pas les émissions. – siu07

+0

Salut, je l'ai fait fonctionner. J'ai changé cette ligne Intent intention = new Intent (this, myMapView.class); Intention intention = new Intention(); et ça a marché – siu07

0

AndroidManifest.xml devrait inclure:

<uses-permission 
    android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission 
    android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Au lieu d'utiliser essayer récepteur de radiodiffusion:

private LocationManager locationManager; 
    private GeoUpdateHandler geoUpdateHandler; 

    public void onCreate(Bundle bundle) { 
      super.onCreate(bundle); 
      geoUpdateHandler = new GeoUpdateHandler(); 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
        0, geoUpdateHandler); 
} 

public void onStop() 
    { 
     locationManager.removeUpdates(geoUpdateHandler); 
     super.onStop(); 

    } 

public class GeoUpdateHandler implements LocationListener { 

     private boolean _bLocationChangeReceived = false; 

     public void onLocationChanged(Location location) { 
      int lat = (int) (location.getLatitude() * 1E6); 
      int lng = (int) (location.getLongitude() * 1E6); 
      GeoPoint point = new GeoPoint(lat, lng); 
      if(!_bLocationChangeReceived) 
      { 
      mapController.animateTo(point); // mapController.setCenter(point); 
      _bLocationChangeReceived = true; 
      } 
      OverlayItem overlayitem = new OverlayItem(point, "Testing", "My location!"); 
      itemizedoverlay.resetOverlay(overlayitem, 0); 
     } 

     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 
Questions connexes