【发布时间】:2012-11-16 04:43:29
【问题描述】:
我在使用/理解 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"
}
...
【问题讨论】:
-
发布异常堆栈
-
请使用 LogCat 并查看与您的“错误”相关的堆栈跟踪。如果您不理解,请复制并通过上面的“编辑”链接将其粘贴到您的问题中。
标签: android broadcastreceiver intentfilter android-pendingintent proximity