【发布时间】:2019-07-15 18:13:25
【问题描述】:
我正在尝试运行访问本地存储的服务,查找哪些项目尚未上传到服务器,然后上传它们。在尝试分配我的文件名时,我一直在我的活动中使用 filesDir。我相信 filesDir 需要一个上下文,而我正在制作的服务没有。有没有替代方案?这是我的代码:
class AuthorizationSaveTask : AsyncTask<Int, Int, String>() {
override fun onPreExecute() {
}
override fun doInBackground(vararg params: Int?): String {
val startId = params[0]
//Get data for list view
var authorizationArrayList = ArrayList<AuthorizationObject>()
try {
//Set target file to authorizations.txt
val targetFile = File(filesDir, "authorizations.txt")
//Create new file input stream
val fis = FileInputStream(targetFile)
//Create new object input stream, using fis
val ois = ObjectInputStream(fis)
//write object input stream to object
//TODO: There has to be a better syntax than this.
authorizationArrayList = (ois.readObject() as ArrayList<*>).filterIsInstance<AuthorizationObject>() as ArrayList<AuthorizationObject>
//close object output stream
ois.close()
//close file output stream
fis.close()
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return "Service complete $startId"
}
override fun onProgressUpdate(vararg values: Int?) {
super.onProgressUpdate(*values)
val counter = values[0]
Log.i("BEAU", "Service Running $counter")
}
override fun onPostExecute(result: String) {
Log.i("BEAU", result)
}
感谢您的宝贵时间!如果我可以提供任何其他信息,请告诉我。
【问题讨论】:
-
明白了!我不会再这样做了。
标签: java android service kotlin android-asynctask