我一直在测试在我的应用程序中发送和接收广播,但我遇到了问题。该服务开始,我得到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();
}
};
答案 0 :(得分:2)
触发的意图没有动作集。当你在IntentFilter中调用“addAction”时:
IntentFilter filter = new IntentFilter();
filter.addAction(GEO_LNG);
filter.addAction(GEO_LAT);
registerReceiver(locationRec, filter);
您正在添加接收方将侦听的操作(在这种情况下,接收方将侦听GEO_LNG和GEO_LAT。
在您触发意图的服务中,意图需要包含该操作,以便接收者发现它。确定要发送的是哪一个,然后将Intent-firing代码修改为如下所示:
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);
...
答案 1 :(得分: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) {
}
}