从Java应用程序类中在Kotlin服务中获取值。

huangapple 未分类评论48阅读模式
英文:

Get value from java application class in kotlin service

问题

以下是翻译好的部分:

我有一个 Java 应用程序类,我想在 Kotlin 服务中从它获取一个值。

我尝试使用以下代码来实现,但返回值为空:

myValue = (application as App).getNumValue

部分代码示例:

应用程序类:

private String numValue = "";

public String getNumValue() {
    return numValue;
}

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

用于设置值的 Java 活动:

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

Kotlin 服务和读取值:

myValue = (application as App).getNumValue()

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

i have a java application class and i want to get a value from it in kotlin service .

i try to do it with this code but it's return null

myValue = (application as App).getNumValue

some of my code

Application Class:

private String numValue = "";


public String getNumValue() {
    return numValue;
}

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

java Activity for set value :

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

Kotlin Service And Read Value :

myValue = (application as App).getNumValue()

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

答案1

得分: 0

以下是翻译好的部分:

我已经解决了我的问题:

val myValue = (this.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()

huangapple
  • 本文由 发表于 2020年5月3日 13:12:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61569913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定