3

我一直在测试在我的应用程序中发送和接收广播,但我遇到了问题。服务启动,我让 Toast 说广播已发送,但在 myMapView 类中从未调用 onReceive。我只在下面包含了我认为相关的代码......任何帮助将不胜感激。我认为我没有正确注册接收器。

提前致谢。

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


    }
};
4

2 回答 2

3

触发的意图没有动作集。当您在此处的 IntentFilter 中调用“addAction”时:

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

您正在添加接收器将侦听的操作(在这种情况下,接收器将侦听 GEO_LNG 和 GEO_LAT。

在您触发意图的服务中,意图需要包含该操作,以便接收器发现它。决定要发送哪一个,然后修改 Intent 触发代码,如下所示:

        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);
        ...
于 2010-11-04T18:05:22.690 回答
0

AndroidManifest.xml 应包括:

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

而不是使用广播接收器尝试:

    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) {
        }
    }
于 2010-11-04T17:53:35.680 回答