Reputation: 447
I have one class that extends BroadcastReceiver
. So when a message arrives that method is called. Now in that method I am calling another class's method to set the text in an EditText
. But since I am using kotlin I need to use a static method in a companion object. So how do I call setText
for my EditText
in that method or somewhere else?
My BroadcastReceiver
Class :
class SMS : BroadcastReceiver() {
val OTP_REGEX = "[0-9]{1,6}"
override fun onReceive(context: Context, intent: Intent) {
Log.e("onReceive", "==->$intent")
val bundle = intent.extras
try {
if (bundle != null) {
Log.e("onReceive", "==->$bundle")
val pdusObj = bundle.get("pdus") as Array<Any>
for (i in pdusObj.indices) {
val currentMessage = SmsMessage.createFromPdu(pdusObj[i] as ByteArray)
val phoneNumber = currentMessage.displayOriginatingAddress
val senderNum = phoneNumber
val message = currentMessage.displayMessageBody
Log.e("getMessage", "===->$message")
try {
VerifyOTPActivity.ReceievdMsg(message);
} catch (e: Exception) {
e.printStackTrace()
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
The class where I need to set that message in edit text.
class VerifyOTPActivity : AppCompatActivity() {
companion object {
@JvmStatic
fun ReceievdMsg(message: String) {
var otp = ""
val OTP_NUMBERS = "[0-9]{1,6}"
val pattern = Pattern.compile(OTP_NUMBERS)
val matcher = pattern.matcher(message)
while (matcher.find()) {
otp = matcher.group()
}
try {
// edt.setText(otp) // Edit text
} catch (e: Exception) {
}
}
}
var mobileNo: String = ""
var preference: AppPreference? = null
lateinit var adminAPI: AdminAPI
lateinit var customDialog: CustomDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_verify_otp)
preference = Utils.getAppPreference(applicationContext)
adminAPI = ServiceGenerator.getAPIClass()
customDialog = CustomDialog(this)
customDialog.setCancelable(false)
val intent: Intent = intent
mobileNo = intent.getStringExtra("mobileNo")
workonIds()
setupToolBar()
}
private fun setupToolBar() {
txt_toolbar_title.gravity = Gravity.CENTER
}
private fun workonIds() {
txt_mobile_no.setText(mobileNo)
txt_mobile_no.setOnClickListener{
onBackPressed()
}
layout_next.setOnClickListener {
val getOTP = edt_otp.text.toString().trim()
val getMobileNo = txt_mobile_no.text.toString().trim()
if (getOTP.isEmpty()) {
C.showToast(applicationContext, resources.getString(R.string.emptyotp))
} else if (getOTP.length < 6) {
C.showToast(applicationContext, resources.getString(R.string.validotp))
} else {
verifyOTP(getMobileNo , getOTP)
}
}
}
private fun verifyOTP(mobileNo: String, otp: String) {
customDialog.show()
val sendOTP : Call<AppModel> = adminAPI.VerifyOTP(mobileNo , otp)
sendOTP.enqueue(object : Callback<AppModel> {
override fun onFailure(call: Call<AppModel>?, t: Throwable?) {
customDialog.dismiss()
C.errorToast(applicationContext , t)
}
override fun onResponse(call: Call<AppModel>?, response: Response<AppModel>?) {
customDialog.dismiss()
val appModel : AppModel = response!!.body()!!
if (appModel != null) {
if (appModel.isStatus) {
val intent = Intent(applicationContext, UserRegistrationActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
preference!!.verify = true
preference!!.mobileNumber = mobileNo
intent.putExtra("mobileNo", mobileNo)
startActivity(intent)
} else {
C.showToast(applicationContext , appModel.message)
}
} else {
C.defaultError(applicationContext)
}
}
})
}
}
How do I set that message in my EditText
?
Upvotes: 1
Views: 844
Reputation: 7649
You can setup an event bus that is a singleton. That way from your BroadcastReceiver
you can send events to the bus and from your Activity
/Fragment
/Whatever
you can subscribe to those events and react accordingly
You can check some libraries like EventBus or Otto although I strongly recommend RxJava. It's a bit more complicated but way more powerful
Upvotes: 1
Reputation: 733
You can create inner class in VerifyOTPActivity
and extend it with BroadcastReceiver
. Than inside your onReceive()
you will have access to your VerifyOTPActivity
instance and will be able to call findViewById()
to get your TextView
instance to write your text there.
Of course you should call registerReceiver()
inside onCreate()
of your VerifyOTPActivity
and unregisterReceiver()
inside onDestroy()
In other words just make your SMS
braodcast receiver inner to VerifyOTPActivity
and bind its registration to the activity lifecycle
Upvotes: 1