4

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

I am new to Android.

Marwan Tushyeh
  • 1,505
  • 4
  • 24
  • 47
  • You can take a look at this post http://stackoverflow.com/questions/4660823/android-alarm-not-working/4661122#4661122 – ccheneson Jan 18 '13 at 10:53
  • There is plenty of answers on this topic. Check [this](http://stackoverflow.com/a/4805733/1521064), [this](http://stackoverflow.com/questions/4134203/how-to-use-registerreceiver-method) and [this](http://stackoverflow.com/questions/6508300/how-to-register-broadcast-receiver). – vault Jan 18 '13 at 11:00

4 Answers4

5

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);
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

For API level 19 and above

 IntentFilter smsFilter = new IntentFilter();
 smsFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(this.receiver, filter);
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
0
private BroadcastReceiver receiver = new BroadcastReceiver() {

   @Override
   public void onReceive(Context context, Intent intent) {
       //some action
   }
};

IntentFilter filter = new IntentFilter("YOUR_ACTION");
registerReceiver(receiver, filter);

Hope this helps

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
Priyanka
  • 381
  • 1
  • 6
  • 8
-1

As you've asked to register BroadcastReceiver inside your MainActivity that fits to registerReceiver-codewise-inside-MainActivity

Here's what code looks like ==>

public class MainActivity extends Activity 
{
BroadcastReceiver smsReceiver;
private IntentFilter myFilter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);

   protected void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    registerReceiver(smsReceiver, myFilter); // register BroadCastReceiver at time of initialization of Activity with proper Intent-Filter
   }

@Override
  protected void onPause() 
   {
    unregisterReceiver(smsReceiver); // UnRegister BroadCastReceiver as you no longer have your activity at Foreground -- Saving CPU & Battery Drainage
    super.onPause();
   }

 @Override
   protected void onResume() 
   {
    registerReceiver(smsReceiver, myFilter); // Register BroadCastReceiver Once again as your activity comes from pause to forground state again.
    super.onResume();
   }

  smsReceiver = new BroadcastReceiver() //Implementation of your BroadCastReceiver
  {

    @Override
    public void onReceive(Context context, Intent intent)
    {
       // Do whatever you like as sms is received and caught by these BroadCastReceiver
        Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();          
    }
  };
 }
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96