android - Get message from broadcast receiver to activity -
i know question has been asked many times still unable solve problem.i want otp sms in edittext of activity.for using broadcast receiver.
code broadcast receiver:
private static final string tag = receivesms.class.getsimplename(); private smsreceivedlistner smsreceived = null; @override public void onreceive(context context, intent intent) { //code sms.... log.e(tag, "otp received: " + verificationcode); if (smsreceived != null) { smsreceived.onsmsreceived(verificationcode); } else { if (constants.isloggingenable) { logger.logerror(tag, "sms listner null"); } } } } } catch (exception e) { log.e(tag, "exception: " + e.getmessage()); } } public void setonsmsreceivedlistener(context context) { this.smsreceived = (smsreceivedlistner) context; }
activity code
public class enterotp extends masteractivity implements view.onclicklistener, ontaskcomplete, smsreceivedlistner { private static final string tag = enterotp.class.getsimplename(); private context context; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.otp); context = this; init(); } private void init() { setuptoolbar(); receivesms = new receivesms(); receivesms.setonsmsreceivedlistener(this); }
i have used interface getting null
.so can otp.
p.s- dont want start new activity via intent because activity running only, if via intent can pass otp without starting new activity , maintaing stack well?
if want receive sms when activity running use code:
private void init() { receivesms = new receivesms(); receivesms.setonsmsreceivedlistener(this); registerreceiver(receivesms, new intentfilter("android.provider.telephony.sms_received")); }
and remove receiver androidmanifest.xml
i hope helped you.
edit:
in ondestroy must use code:
protected void ondestroy() { super.ondestroy(); // ... unregisterreceiver(receivesms); }
Comments
Post a Comment