use registerReceiver for non activity and non service class

前端 未结 3 515
悲&欢浪女
悲&欢浪女 2021-01-14 12:01

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

But the class

相关标签:
3条回答
  • 2021-01-14 12:21

    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.

    0 讨论(0)
  • 2021-01-14 12:31

    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.

    0 讨论(0)
  • 2021-01-14 12:32

    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));
    
    0 讨论(0)
提交回复
热议问题