【发布时间】:2019-02-16 13:19:46
【问题描述】:
我编写了一个 kotlin 类,其中包含一个重写的乐趣和一个将 var 更新到类范围的乐趣(可悲的是,我是 Kotlin 的新手!)
class mySampleClass: sampleReference(){
var varToBeUpdated:String = "my string" //var in class scope to be updated
fun updateMyVar(gotString:String){
//I tried this, it didn't work
this.varToBeUpdated = gotString
// also this didn't work
varToBeUpdated = gotString
}
override fun sample(context: Context, intent: Intent){
//here i need my varToBeUpdated with new string
runSomeThing(varToBeUpdated)
//some work to be done here
}
}
在我调用方法的地方:
myObject.updateMyVar("new string")
myObject.sample()
我想知道如何更新我需要的 var,因为我无法在“有趣的示例”中添加新参数,因为它覆盖了类方法
提前致谢,向大家致以最诚挚的问候:)
更新:添加我的实际代码,因为当我调用覆盖方法时,该类似乎无法保持正确的更新值:
这是我的 BroadcastReceiver,检查何时下载完成并执行一些操作
class DownloadBroadcastManager: BroadcastReceiver() {
var myClassFilename:String = "default"
var myClassExtension:String = ".default"
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
//Show a notification
// here there's a log to check if var are updated
println("myTag - variables $myClassFilename, $myClassExtension")
Toast.makeText(context, "Download of $myClassFilename$myClassExtension completed", Toast.LENGTH_LONG).show()
// richiama azioni come player o display image o altro?
//player
var uri = Uri.parse (Environment.getExternalStorageDirectory().getPath() + "/Download/$myClassFilename$myClassExtension") //myClassExtension is ".mp3", dot is included, however it seems class is re-intialized as i call the method
println("myTag - uri: $uri")
println("myTag - context: $context")
var mPlayer = MediaPlayer() // I added this declaration (that's be re-done later) cause I had a problem in making the player running (of course giving it a valid path to a valid file). Now this is "junk code"
mPlayer.stop()
mPlayer.reset()
mPlayer.release()
mPlayer = MediaPlayer.create(context, uri) // here there's the proper declaration + initialization
mPlayer.start()
}
}
}
这是我的 DownloaderClass 中的部分...
var brReceiver = DownloadBroadcastManager()
// shows when download is completed
println("myTag - ${brReceiver.myClassFilename}, ${brReceiver.myClassExtension}: originals") //here shows the default: it's right
val intent = Intent(context, MainActivity::class.java)
brReceiver.myClassFilename = myTitle // inject filename
brReceiver.myClassExtension = ".mp3" // inject file extension
println("myTag - ${brReceiver.myClassFilename}, ${brReceiver.myClassExtension}: modified") // here it shows my class property as correctly updated
brReceiver.onReceive(context, intent) // here, as calling the override fun, it get back to default value of the property
【问题讨论】:
-
如果您将
varToBeUpdated公开,您可以使用myObject.varToBeUpdated=“my string”访问它 -
也许我现在的问题是我没有公开它?嗯:无事可做。当我调用该方法时,有些东西再次声明了我的 var,或者只是有些东西阻止我更新它们
-
var mPlayer = MediaPlayer() // I added this declaration (that's be re-done later) cause I had a problem in making the player running (of course giving it a valid path to a valid file). Now this is "junk code"你应该删除它。 -
如果您调用
brReceiver.onReceive(...),DownloadBroadcastManager中的值将被更新。但你不应该那样做。 Android 框架会为您调用它。并且当它发生时,会创建DownloadBroadcastManager类的新实例并设置默认值。我们使用 Intent 将数据传递给 BroadcastReceiver,例如创建广播接收器时调用intent.putExtra("filename", "yourFileName"),并在onReceive()函数中调用intent.getStringExtra("filename")。 stackoverflow.com/questions/10032480/… -
真的非常感谢!
标签: android kotlin broadcastreceiver android-broadcastreceiver