广播接收器onReceive()从未被调用 - Android

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


    }
};
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);
        ...

谢谢您!我觉得我快做完了,请您看一下我的编辑,看看您的想法如何?我仍然无法接收广播。 - siu07
嗨,我搞定了。我更改了这一行代码 Intent intent = new Intent(this, myMapView.class); 为 Intent intent = new Intent(); 然后就可以工作了。 - siu07

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) {
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接