call activity method from broadcast receiver

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

Leave a Comment