Marwan Tushyeh Marwan Tushyeh - 27 days ago 5
Android Question

How to register a receiver in the main activity?

I have a

class that I want to register in the main activity, what exactly should I do ?

I am new to Android.


Either you can do 2 things:

  1. Create and define BroadcastReceiver in the Manifest
  2. Create and register the BroadcastReceiver in code.

For option 2 (which you are asking):

Create a BroadcastReceiver in code (MyBroadcastReceiver). Declare MyBroadcastReceiver in the scope of your Activity:

MyBroadcastReceiver mMyBroadcastReceiver;

Register the BroadcastReceiver in your Activity by:

IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(mMyBroadcastReceiver, filter);