如何使用 putextra 传递自定义列表
How to pass custom list with putextra
我正在尝试使用 putextra 通过我的服务传递列表数据。我没有做。我该怎么做。
fun startService(context: Context, path: String,action: String,list:List<Musics>) {
val startIntent = Intent(context, ForegroundService::class.java)
startIntent.putExtra("path", path)
startIntent.putExtra("action",action)
ContextCompat.startForegroundService(context, startIntent)
}
以上代码是我的startService代码
@Entity(tableName = "musics")
data class Musics(
@PrimaryKey(autoGenerate = true) var uid:Int= 0,
@ColumnInfo(name = "file_name") val filename:String,
@ColumnInfo(name = "file_path") val filepath:String
)
我的数据class
我想传递我的列表(音乐)数据。
在不同组件之间传递大型数据集的最佳方法是将数据存储在本地数据库中,并在 Intent
中传递一些密钥,然后使用该密钥检索数据。
但是,如果您仍然需要按照自己的方式进行操作,请按照下面列出的步骤进行操作
Make Musics class 实现可序列化接口
data class Musics : Serializable
启动服务时你可以做
val yourList: ArrayList<Musics>
startIntent.putExtra("myList", yourList)
在您的服务中,从 Intent 获取列表如下
val list: List<Musics> = getIntent().getSerializableExtra("myList") as List<Musics>
我正在尝试使用 putextra 通过我的服务传递列表数据。我没有做。我该怎么做。
fun startService(context: Context, path: String,action: String,list:List<Musics>) {
val startIntent = Intent(context, ForegroundService::class.java)
startIntent.putExtra("path", path)
startIntent.putExtra("action",action)
ContextCompat.startForegroundService(context, startIntent)
}
以上代码是我的startService代码
@Entity(tableName = "musics")
data class Musics(
@PrimaryKey(autoGenerate = true) var uid:Int= 0,
@ColumnInfo(name = "file_name") val filename:String,
@ColumnInfo(name = "file_path") val filepath:String
)
我的数据class
我想传递我的列表(音乐)数据。
在不同组件之间传递大型数据集的最佳方法是将数据存储在本地数据库中,并在 Intent
中传递一些密钥,然后使用该密钥检索数据。
但是,如果您仍然需要按照自己的方式进行操作,请按照下面列出的步骤进行操作
Make Musics class 实现可序列化接口
data class Musics : Serializable
启动服务时你可以做
val yourList: ArrayList<Musics>
startIntent.putExtra("myList", yourList)
在您的服务中,从 Intent 获取列表如下
val list: List<Musics> = getIntent().getSerializableExtra("myList") as List<Musics>