Marwan Tushyeh Marwan Tushyeh - 1 year ago 91
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.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download