use registerReceiver for non activity and non service class

不问归期 提交于 2019-12-01 21:39:38

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

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.

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.

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