我如何通过一种方法更新整个kotlin类中的var,然后使用另一个方法调用来更新它

时间:2019-02-16 13:19:46

标签: android kotlin broadcastreceiver android-broadcastreceiver

我编写了一个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,因为我无法在“有趣的示例”中添加新的args,因为它覆盖了类方法。

在此先感谢大家:)



更新:添加我的实际代码,原因是该类在我调用覆盖方法时似乎无法保持正确的更新值:

这是我的BroadcastReceiver,用于检查whem下载是否已完成并且他们执行了一些操作

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

3 个答案:

答案 0 :(得分:1)

根据kotlin的文档,您可以为任何变量定义gettersetter方法,如下所示:

var <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]

在您的情况下,可能是这样的:

var varToBeUpdated:String = "my string"
    get() = field
    set(value) { field = value }

答案 1 :(得分:1)

您可以执行以下操作:

  1. 摆脱updateMyVar函数:

    class MySampleClass: SampleReference(){
        var varToBeUpdated:String = "my string" //var in class scope to be updated
    
        override fun sample(context: Context, intent: Intent){
            //here i need my varToBeUpdated with new string
            runSomeThing(varToBeUpdated)
            //some work to be done here
        }
    }
    
  2. 直接更新varToBeUpdated属性:

    val myObject = MySampleClass()
    myObject.varToBeUpdated = "new string"
    myObject.sample()
    

更新: 如果调用brReceiver.onReceive(...),则DownloadBroadcastManager中的值将更新。但是你不应该那样做。 Android框架为您调用。当发生这种情况时,将创建DownloadBroadcastManager类的新实例并设置默认值。我们使用 Intents 将数据传递到BroadcastReceiver,例如在创建intent.putExtra("filename", "yourFileName")时调用BroadcastReceiver,并在intent.getStringExtra("filename")函数中调用onReceive()以获取值。 Here is how to pass/get data to/from BroadcastReceiver

答案 2 :(得分:0)

好,首先要感谢M.SamiAzar,尤其是谢尔盖(Sergey)的回答和令人难以置信的耐心! 不幸的是,当它由框架重新初始化时,BroadcastReceiver也会丢失我之前放入Intent变量中的所有额外内容。 我终于解决了这个问题,让我可以检索所需的字符串,仅将一行文本写入内部存储中的文件中,然后在我的BroadcastReceiver类中进行检索。 这是代码:

这是我在BroadcastReceiver类中的“ onReceive”方法

 override fun onReceive(context: Context, intent: Intent) {
    val action = intent.action
    Log.i("Receiver", "myTag - Broadcast received: " + action)
    var myFilename = "deafult"

    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {

        // read the previously created file from internal storage
        var fileInputStream: FileInputStream? = null
        fileInputStream = context.openFileInput("storeDownloadedData")
        var inputStreamReader: InputStreamReader = InputStreamReader(fileInputStream)
        val bufferedReader: BufferedReader = BufferedReader(inputStreamReader)

        // here setting string var and stringbuilder var to catch the text updated outside the while loop
        val stringBuilder: StringBuilder = StringBuilder()
        var text: String? = null
        var sumText:java.lang.StringBuilder? = null
        // while loop for reading the file line by line (in this case the file can contains just one line a time)
        while ({ text = bufferedReader.readLine(); text }() != null) {
            sumText = stringBuilder.append(text)
        }

        // convert stringBuilder to a list of string splitting the original string obtained by file reading
        var secondText:String = "default"
        println("myTag - text: $text, $sumText")
        if (sumText != null){
            secondText = sumText.toString()
            var listFromText = secondText.split(",")
            // set filename to the title contained in the string
            myFilename = listFromText[0]
        }

        //player - finally play the file retrieving the title from the file in internal storage
        var uri = Uri.parse (Environment.getExternalStorageDirectory().getPath() + "/Download/$myFilename.mp3")
        println("myTag - uri: $uri")
        println("myTag - context: $context")

        var mPlayer = MediaPlayer.create(context, uri)
        mPlayer.start()

    }

这是在我的DownloadManager类中添加的代码:

// strore into an internal storage file the data of title and extensione for the file's gonna be downloaded
    val myStoredFile:String = "storeDownloadedData"
    val data:String = "$myTitle,.mp3"
    val fileOutputStream: FileOutputStream
    // write file in internal storage
    try {
        fileOutputStream = context.openFileOutput(myStoredFile, Context.MODE_PRIVATE)
        fileOutputStream.write(data.toByteArray())
    }catch (e: Exception){
        e.printStackTrace()
    }

    // it notifies when download is completed
    val intent = Intent(context, MainActivity::class.java)
    var brReceiver = DownloadBroadcastManager()

我不知道这是否是“ ortodox”,但它似乎在atm上起作用 :)