3

我正在尝试registerReceiver针对 BluetoothDevice 事件,例如ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECTED

但是我使用的类既不registerReceiver扩展Activity类也不扩展类Service- 所以我传递Context给那个类并注册接收器如下

context.registerReceiver(ActionFoundReceiver, 
          new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));

context.registerReceiver(ActionFoundReceiver, 
              new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));

context.registerReceiver(ActionFoundReceiver, 
          new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));

并处理如下事件

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // do some stuff
        }
    }
};

但是当设备连接或断开连接时,我无法追踪是我的意思ActionFoundReceiver是无法被调用

4

3 回答 3

7

我试图通过使用来解决它,但我真的不知道这有多安全,在这种情况下这是否是一个好习惯。

如果有人有更好的答案,请随时发布

context.getApplicationContext().registerReceiver(ActionFoundReceiver, 
                  new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
于 2012-11-07T17:32:05.060 回答
7

这是一个BroadcastReceiver适合我的草图。

你有一些MyBroadcastReceiver可以完成所有工作的类。

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // do some stuff
        }
    }
}

MyBroadcastReceiver托管在您的MyClass(不是活动或服务)中

public class MyClass {

    private BroadcastReceiver myReceiver = null;

    public MyClass(Context context) {
        myReceiver = new MyBroadcastReceiver();
        context.registerReceiver(myReceiver, new IntentFilter(
                BluetoothDevice.ACTION_ACL_CONNECTED));
        context.registerReceiver(myReceiver, new IntentFilter(
                BluetoothDevice.ACTION_ACL_DISCONNECTED));
        context.registerReceiver(myReceiver, new IntentFilter(
                BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
    }

    public void unregisterReceiver(Context context) {
        context.unregisterReceiver(myReceiver);
    }
}

MyClass生命周期由MyActivity(or MyService) 控制。

public class MyActivity extends Activity {

    private MyClass myClass = null;

    @Override
    public void onResume(Bundle savedInstanceState) {
        super.onResume(savedInstanceState);
        // ...
        myClass = new MyClass(this);
    }

    @Override
    public void onPause() {
        if (myClass != null) {
            myClass.unregisterReceiver(this);
            myClass = null;
        }
        super.onPause();
    }
}

这也可以是onCreate/onDestroy或在服务内。

于 2012-11-07T19:21:02.240 回答
1

我有一些类似的问题,我有一个发送短信的接收器,我注册了它但从未打电话。我试图这样做,但从未被调用

actionfounder= new ActionFounderReceiver();
    context.registerReceiver(actionfounder , new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));

好吧,让我感到惊讶的是,当我以这种方式注册它时它会调用它:

context.registerReceiver(new ActionFoundReceiver() , new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));

和内部类:

class ActionFoundReceiver extends BroadcastReceiver() {

   @Override
   public void onReceive(Context context, Intent intent) {
     String action = intent.getAction();
     if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        // do some stuff
     }
   }
}

就在我得到一个新的实例时,它就可以工作了。

希望这可以帮助。

于 2013-06-18T16:15:15.240 回答