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