简体   繁体   English

从 kotlin 服务中的 java 应用程序 class 获取值

[英]Get value from java application class in kotlin service

i have a java application class and i want to get a value from it in kotlin service.我有一个 java 应用程序 class 并且我想在 kotlin 服务中从中获得价值。

i try to do it with this code but it's return null我尝试使用此代码执行此操作,但它返回 null

myValue = (application as App).getNumValue

some of my code我的一些代码

Application Class:应用 Class:

private String numValue = "";


public String getNumValue() {
    return numValue;
}

public void setNumValue(String numValue) {
    this.numValue = numValue;
}

java Activity for set value: java 活动设定值:

 ((App) this.getApplication()).setNumValue(enNum);

Kotlin Service And Read Value: Kotlin 服务和读取值:

myValue = (application as App).getNumValue()

Toast.makeText(this, myValue +"", Toast.LENGTH_SHORT).show()

I was solved my problem with:我解决了我的问题:

val myValue = (this.application as App).getNumValue()

Toast.makeText(this, myValue +"", Toast.LENGTH_SHORT).show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM