我很难使用/理解BroadcastReceivers和IntentFilters。我的测试活动中有以下代码。测试活动包含一个.addProximityAlert,如果.addProximityAlert被触发,我想广播到Test2接收器。我测试这个时发生错误。我究竟做错了什么?为什么我的接收器不接收广播?
测试活动:
public class Test extends BroadcastReceiver
{
LocationManager lm;
...
@Override
public void onReceive(Context context, Intent intent)
{
...
final String PROX_ALERT_INTENT = "com.example.proxalert.Test2";
Intent alert = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(context, 0, alert, 0);
lm.addProximityAlert(latitude, longitude, radius, expiration, proximityIntent);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
context.registerReceiver(new Test2(), filter);
Test2的接收机:
public class Test2 extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = arg1.getBooleanExtra(key, false);
if (entering) {
//toast notification "welcome"
}
...
后的异常堆栈 – Atrix1987
请使用logcat的,看你的“错误”相关的堆栈跟踪。如果您不明白,请将其复制并通过上面的“编辑”链接粘贴到您的问题中。 – CommonsWare