Java 如何在kotlin中使用非静态方法?
我有一个类扩展了Java 如何在kotlin中使用非静态方法?,java,android,kotlin,static-methods,Java,Android,Kotlin,Static Methods,我有一个类扩展了BroadcastReceiver。因此,当消息到达时,将调用该方法。现在在该方法中,我调用另一个类的方法来设置EditText中的文本。但是因为我使用的是kotlin,所以我需要在一个伴随对象中使用一个静态方法。那么,如何在该方法中或其他地方为我的EditText调用setText MyBroadcastReceiver类别: class SMS : BroadcastReceiver() { val OTP_REGEX = "[0-9]{1,6}" ove
BroadcastReceiver
。因此,当消息到达时,将调用该方法。现在在该方法中,我调用另一个类的方法来设置EditText
中的文本。但是因为我使用的是kotlin,所以我需要在一个伴随对象中使用一个静态方法。那么,如何在该方法中或其他地方为我的EditText
调用setText
MyBroadcastReceiver
类别:
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()
}
}
}
class SMS:BroadcastReceiver(){
val OTP_REGEX=“[0-9]{1,6}”
覆盖接收(上下文:上下文,意图:意图){
Log.e(“onReceive”,“==->$intent”)
val bundle=intent.extras
试一试{
if(bundle!=null){
Log.e(“onReceive”,“==->$bundle”)
val pdusObj=bundle.get(“pdus”)作为数组
用于(pdusObj.索引中的i){
val currentMessage=SmsMessage.createFromPdu(pdusObj[i]作为ByteArray)
val phoneNumber=currentMessage.displayOriginatingAddress
val senderNum=电话号码
val message=currentMessage.displayMessageBody
Log.e(“getMessage”,“==->$message”)
试一试{
VerifyOTPActivity.receievdmg(消息);
}捕获(e:例外){
e、 printStackTrace()
}
}
}
}捕获(e:例外){
e、 printStackTrace()
}
}
}
我需要在编辑文本中设置该消息的类
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)
}
}
})
}
}
class-VerifyOTPActivity:appcompativity(){
伴星{
@JvmStatic
fun ReceievdMsg(消息:字符串){
var otp=“”
val OTP_编号=“[0-9]{1,6}”
val模式=模式。编译(OTP_编号)
val matcher=pattern.matcher(消息)
while(matcher.find()){
otp=matcher.group()
}
试一试{
//edt.setText(otp)//编辑文本
}捕获(e:例外){
}
}
}
var mobileNo:String=“”
变量首选项:AppPreference?=null
lateinit var adminAPI:adminAPI
lateinit var customDialog:customDialog
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u verify\u otp)
preference=Utils.getAppPreference(applicationContext)
adminAPI=ServiceGenerator.getApicClass()
customDialog=customDialog(此)
customDialog.setCancelable(错误)
val intent:intent=intent
mobileNo=intent.getStringExtra(“mobileNo”)
修井()
setupToolBar()
}
私人娱乐设置工具栏(){
txt_工具栏_title.gravity=gravity.CENTER
}
私人娱乐活动(){
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.showtoos(applicationContext,resources.getString(R.string.emptyotp))
}否则如果(getOTP.length<6){
C.showtoos(applicationContext,resources.getString(R.string.validotp))
}否则{
验证OTP(getMobileNo,getOTP)
}
}
}
私人娱乐验证otp(mobileNo:String,otp:String){
customDialog.show()
val sendOTP:Call=adminAPI.VerifyOTP(mobileNo,otp)
sendOTP.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用?、t:可丢弃?){
customDialog.disclose()
C.errorToast(应用程序上下文,t)
}
覆盖fun onResponse(调用:调用?,响应:响应?){
customDialog.disclose()
val appModel:appModel=response!!.body()!!
if(appModel!=null){
if(appModel.isStatus){
val intent=intent(applicationContext,UserRegistrationActivity::class.java)
intent.flags=intent.FLAG\u活动\u清除\u顶部
首选项!!.verify=true
首选项!!.mobileNumber=mobileNo
意向。额外(“mobileNo”,mobileNo)
星触觉(意图)
}否则{
C.showtoos(applicationContext,appModel.message)
}
}否则{
C.defaultError(applicationContext)
}
}
})
}
}
如何在我的
EditText
中设置该消息?您可以在VerifyOTPActivity
中创建内部类,并使用BroadcastReceiver
对其进行扩展。而在您的onReceive()
中,您将有权访问您的VerifyOTPActivity
实例,并将能够调用findViewById()
让您的TextView
实例在那里写入文本。
当然,您应该调用VerifyOTPActivity
的registerReceiver()
内部onCreate()
和unregisterReceiver()
内部onDestroy()
换句话说,只需将您的
SMS
braodcast接收器置于VerifyOTPActivity
的内部,并将其注册绑定到活动生命周期即可设置单例事件总线。通过这种方式,您可以从广播接收器
向总线发送事件,也可以从活动
/片段
/任何内容
订阅这些事件并做出相应的反应
您可以查看一些库,如或,尽管我强烈推荐。有点复杂