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

发布于 2024-09-30 15:30:35 字数 3852 浏览 2 评论 0原文

我一直在测试在我的应用程序中发送和接收广播,但我遇到了问题。服务启动,我收到 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();


    }
};

I've been testing sending and receiving broadcasts in my application but I have a problem. The service starts and I get the Toast to say the broadcast has been sent but onReceive is never called in the myMapView class. I've only included the code I think is relevant below... any help would be much appreciated. I don't think I'm registering the receiver correctly.

Thanks in advance.

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


        }
    };

Edit I've modified my code to look like this, but I'm still not having any luck with receiving the broadcasts.

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


    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-10-07 15:30:35

正在触发的意图没有设置操作。当您在此处的 IntentFilter 中调用“addAction”时:

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

The intent that's firing doesn't have an action set. When you call "addAction" in the IntentFilter here:

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

You're adding actions that the receiver will listen for (in this case, the receiver will listen for GEO_LNG and GEO_LAT.

In the service where you fire the intent, the intent needs to contain that action in order for the receiver to spot it. Decide which one you want to send, and then modify the Intent-firing code to look like this:

        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);
        ...
对不⑦ 2024-10-07 15:30:35

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

AndroidManifest.xml should include:

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

Instead of using broadcast receiver try:

    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) {
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文