use registerReceiver for non activity and non service class

三世轮回 提交于 2019-12-20 02:29:36

问题


I am trying to registerReceiver for BluetoothDevice events like ACTION_ACL_CONNECTED , ACTION_ACL_DISCONNECTED.

But the class in which I am using registerReceiver does not extend Activity class nor Service class - so I am passing Context to that class and registering receiver as follows

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));

and handling events as follows

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

but when the device is connected or disconnected I am not able to trace is I mean the ActionFoundReceiver is not able to get called


回答1:


I tried to solve it by using , but i really don't know how safe is this and it is a good practice in this case or not.

if anyone has a better ans feel free to post

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



回答2:


Here is a sketch of a BroadcastReceiver that works for me.

You have some class MyBroadcastReceiver that does all the work.

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 is hosted in your MyClass (that is not an activity or service)

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 lifetime is controlled by 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();
    }
}

This can also be onCreate/onDestroy or within a service.




回答3:


I have some similar problem, i have a receiver for sms sent, i register it but never called. I tried to do so, and was never called

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

Well, what was my surprises, that it called when i register it this way:

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

And the inner class:

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

Just when i get a fresh new instance, it's works.

Hope this helps.



来源:https://stackoverflow.com/questions/13238600/use-registerreceiver-for-non-activity-and-non-service-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!