1

我在使用/理解 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"
    }
    ...
4

4 回答 4

2

测试扩展广播接收器,因此必须通知广播才能激活。你确定那是你想做的吗?

真的是安卓测试吗?我不这么认为。因此,如果您只是删除 Test 类的代码,请使用按钮创建一个活动,并在单击按钮时在 Test 中执行您的代码。

于 2012-11-25T01:10:03.480 回答
1

嗨,在您的活动(或服务)下使用 BroadcastReceivers 和 IntentFilters 来喜欢这个:

             broadcastReceiver = new BroadcastReceiver() {//global BroadcastReceiver
            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();
                //Action receive
            };

意图过滤器:

        IntentFilter intentfilterTime = new IntentFilter(); 
        intentfilterTime.addAction(Intent.ACTION_BATTERY_CHANGED);//receive battery level change
        registerReceiver(broadcastReceiver, intentfilterTime);

添加您想要接收的所有操作。您的代码不起作用,因为您在另一个内部使用广播,我认为这不是您想要的。

于 2012-11-25T18:59:56.437 回答
1

您可以简单地同时注册两个 BroadcastReceiver 并控制启用,通过简单的方法禁用它们,例如

  • 使用静态/非静态布尔变量
  • 使用 Preferences 保存键值

例如:

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);
        Test2.someStaticVariable = true;


public class Test2 extends BroadcastReceiver {
    public static boolean someStaticVariable;

    @Override
    public void onReceive(Context arg0, Intent arg1) {

    if (!someStaticVariable){
        return;
    }

    String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = arg1.getBooleanExtra(key, false);
        if (entering) {
        //toast notification "welcome"
    }
    ...
于 2012-11-28T05:44:36.023 回答
0

我能够通过在 android 清单中注册接收器来使其工作,而不是尝试在 test1 中动态注册接收器。这个网站真的很有帮助:

http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers

于 2012-12-17T17:40:27.093 回答