使用registerReceiver非活性和非服务类(use registerReceiver fo

2019-07-03 14:33发布

我想registerReceiver像BluetoothDevice类事件ACTION_ACL_CONNECTEDACTION_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不能被调用

Answer 1:

我试图通过解决这个问题,但我真的不知道如何安全是这一点,正是在这样的情况下,或者不是一个好的做法。

如果任何人有更好的ANS随意张贴

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


Answer 2:

这里是一个草图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(this);
    }
}

MyClass生存期由控制MyActivity (或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或服务中。



Answer 3:

我有一些类似的问题,我有发来的短信接收器,我注册了,但从来没有所谓。 我试图这样做,也从来没有所谓的

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
     }
   }
}

只是当我得到一个全新的实例,它的作品。

希望这可以帮助。



文章来源: use registerReceiver for non activity and non service class