gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:42:26 25 4
gpt4 key购买 nike

我一直在我的应用程序中测试发送和接收广播,但我遇到了问题。服务启动,我得到 Toast 表示广播已发送但 onReceive 从未在 myMapView 类中调用。我只在下面包含了我认为相关的代码...任何帮助将不胜感激。我认为我没有正确注册接收器。

提前致谢。

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


}
};

最佳答案

正在触发的 Intent 没有 Action 集。当您在此处的 IntentFilter 中调用“addAction”时:

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

您正在添加接收器将监听的操作(在本例中,接收器将监听 GEO_LNG 和 GEO_LAT。

在您触发 Intent 的服务中, Intent 需要包含该操作以便接收者发现它。决定要发送哪一个,然后将 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);
...

关于android - 从未调用过广播接收器 onReceive() - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4099499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com