广播接收器 onReceive() 从未被调用 - Android
我一直在测试在我的应用程序中发送和接收广播,但我遇到了问题。服务启动,我收到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正在触发的意图没有设置操作。当您在此处的 IntentFilter 中调用“addAction”时:
您正在添加接收器将侦听的操作(在本例中,接收器将侦听 GEO_LNG 和 GEO_LAT。
在您触发意图的服务中,意图需要包含决定要发送哪个操作,然后将意图触发代码修改为如下所示:
The intent that's firing doesn't have an action set. When you call "addAction" in the IntentFilter here:
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:
AndroidManifest.xml 应包括:
尝试使用广播接收器而不是:
AndroidManifest.xml should include:
Instead of using broadcast receiver try: