我在使用/理解 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);
测试2接收器:
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"
}
...