2010-11-04 52 views
3

我一直在測試發送和接收廣播在我的應用程序,但我有一個問題。該服務開始,我得到吐司說廣播已發送,但onReceive從未在myMapView類中調用。我只包括我認爲在下面的代碼...任何幫助將不勝感激。我認爲我沒有正確註冊接收器。廣播接收器onReceive()從未呼叫 - Android

在此先感謝。

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


     } 
    }; 

編輯我修改我的代碼看起來像這樣,但我仍然不具有接收廣播的任何運氣。

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


    } 
}; 

回答

2

說的射擊沒有一個動作組的意圖:

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

使用廣播接收器嘗試代替。當你打電話「的addAction」在這裏的IntentFilter。

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

您要添加的動作,接收器將偵聽(在這種情況下,接收器將監聽GEO_LNG和GEO_LAT

在服務在那裏你火的意圖,意圖需要包含,以便採取行動,以便接收機發現它決定你想要哪一個發送,然後修改意向燒製代碼看起來像這樣:

 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

喜感謝這!我想我快到了,請你看看我的編輯並看看你的想法?仍然沒有得到廣播.. – siu07 2010-11-04 20:00:58

+0

嗨,我得到它的工作。我改變了這一行Intent intent = new Intent(this,myMapView.class);意向意圖=新的意圖();它的工作 – siu07 2010-11-04 23:37:56

0

AndroidManifest.xml中應包括:

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) { 
     } 
    } 
相關問題