17

In the main activity, a layout is loaded that has some input fields and a submit button. When the submit button is clicked, the onClick handler method sends an sms back to the same mobile number :

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, "hi", null, null);

There is a broadcast receiver defined that intercepts the message :

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Bundle pdusBundle = intent.getExtras();
    Object[] pdus=(Object[])pdusBundle.get("pdus");
    SmsMessage messages=SmsMessage.createFromPdu((byte[]) pdus[0]);
    if(messages.getMessageBody().contains("hi")){
        abortBroadcast();
    }

}
}

Now, from the broadcast receiver, I want to call a function(with parameter), which is within my main activity. Is that possible? If yes, what kind of code should i add in my broadcast receiver ?

faizal
  • 3,497
  • 7
  • 37
  • 62
  • 2
    [This](http://stackoverflow.com/questions/16934425/call-an-activity-method-from-a-broadcastreceiver-class) should help – Manishika Sep 20 '13 at 18:19

5 Answers5

24

Thanks @Manishika. To elaborate, making the Broadcastreceiver dynamic, instead of defining it in the manifest, did the trick. So in my broadcast receiver class, i add the code :

MainActivity main = null;
void setMainActivityHandler(MainActivity main){
    this.main=main;
}

In the end of the onReceive function of the BroadcastReceiver class, I call the main activity's function :

main.verifyPhoneNumber("hi");

In the main activity, I dynamically define and register the broadcast receiver before sending the sms:

SmsReceiver BR_smsreceiver = null;
BR_smsreceiver = new SmsReceiver();
BR_smsreceiver.setMainActivityHandler(this);
IntentFilter fltr_smsreceived = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(BR_smsreceiver,fltr_smsreceived);  
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
faizal
  • 3,497
  • 7
  • 37
  • 62
  • Hello @faizal, I need to keep my BroadcastReceiver running all the time even if my app is not visible. I am able to do that with the configs in menifext.xml file. And I also need to call my activity method from my BroadcastReceiver. I am able to call my activity method with your solution provided above but with this my BroadcastReceiver is stopped once I am out of my app. And with my own config, my BroadcastReceiver is running but I am not able to call my Activity method. Any idea how to get it done both. Thanks Jai. – Jaikrat Oct 05 '15 at 19:30
8

Pass your Activity's context to BroadcastReceiver's contructor.

public class SmsReceiver extends BroadcastReceiver{

    MainActivity ma; //a reference to activity's context

    public SmsReceiver(MainActivity maContext){
        ma=maContext;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ma.brCallback("your string"); //calling activity method
    }

}

and in your MainActivity

public class MainActivity extends AppCompatActivity {
    ...
    public void onStart(){
        ...        
    SmsReceiver smsReceiver = new SmsReceiver(this); //passing context
    LocalBroadcastManager.getInstance(this).registerReceiver(smsReceiver,null);
        ...
    }

    public void brCallback(String param){
        Log.d("BroadcastReceiver",param);
    }
}

hope it helps

junhaotee
  • 483
  • 4
  • 10
  • LocalBroadcastManager does not work nowadays, I had to use `IntentFilter filter = new IntentFilter(); filter.addAction("com.google.android.gms.auth.api.phone.SMS_RETRIEVED"); this.registerReceiver(smsReceiver, filter);` – mikep Jul 19 '21 at 14:17
2

use this

Intent intent=new Intent();
intent.setClassName("com.package.my", "bcd.class");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);
vaibhav kumar
  • 414
  • 2
  • 11
  • 1
    I cannot use your solution as i do not want to start a new main activity. I have posted an answer with the solution. – faizal Sep 21 '13 at 06:52
0

You can't directly call a function in your Activity unless it's a public static method but don't do so. I recommend this:

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = new Bundle();
    bundle.putExtraString("ACTION", "stopBroadcast");

    // ### put what ever you need into the bundle here ###

    Intent intent = new Intent();
    intent.setClassName(context, "activity.class");
    intent.putExtras(bundle);

    context.startActivity(intent);
}

And then from your Activity's onCreate() get Bundle and take your actions as needed.

Mustafa Berkay Mutlu
  • 1,929
  • 1
  • 25
  • 37
JanithaR
  • 1,094
  • 2
  • 11
  • 23
  • 1
    Yes, you are right. The function in my main activity that needs to be called, has to be public. I cannot use your solution as i do not want to start a new main activity. I have posted an answer with the solution. – faizal Sep 21 '13 at 06:51
-4

It's a little bit late, but nobody mentioned it. There is no need for passing activity. As @faizal specifies, IntentService is started from MainActivity, so, context in onReceive() is already instance of MainActivity. It is enough to write smth like this:

@Override
public void onReceive(Context context, Intent intent) {
    if(context instanceof MainActivity) {
        MainActivity activity = (MainActivity) context;
    }
}
  • 1
    java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to com.mobiquel.udhampur.ui.home.HomeActivity This will crash app. – Arman Reyaz Oct 01 '19 at 09:28