我正试图用putextra通过我的服务传递List数据。我没有那样做。我怎么能那样做。
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)
}
以上代码是我的启动服务代码
@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
)
我的数据类
我想传递我的列表(音乐(数据。
在不同组件之间传递大型数据集的最佳方法是将数据存储在本地数据库中,并在Intent
中传递一些密钥,然后使用该密钥检索数据。但如果你仍然需要这样做,按照下面列出的步骤
使Musics类实现可串行化接口
data class Musics : Serializable
启动服务时,您可以执行
val yourList: ArrayList<Musics>
startIntent.putExtra("myList", yourList)
在您的服务中,从Intent获取您的列表,如下
val list: List<Musics> = getIntent().getSerializableExtra("myList") as List<Musics>