如何在fillInIntent中设置数据

问题描述 投票:0回答:1

我的主屏幕小部件中有一个列表视图,我想在单击该项目时启动一个挂起的意图。我参考了this并为每个列表项设置了一个fillInIntent,然后设置了一个未决的意图模板,它触发了小部件的广播接收器:

class WidgetProvider : HomeWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, widgetData: SharedPreferences) {

        appWidgetIds.forEach { widgetId ->
            val views = RemoteViews(context.packageName, R.layout.widget_layout).apply {

                val todosStr  = widgetData.getString("todos", "null")
                val todos = ArrayList<HashMap<String, Any>>()
                val todosRemoteView = RemoteViews.RemoteCollectionItems.Builder()
                if(todosStr != "null"){
                    val jObj = JSONObject(todosStr)
                    val jsonArry = jObj.getJSONArray("todos")
                    for (i in 0 until jsonArry.length()) {
                        val todo = HashMap<String, Any>()
                        val obj = jsonArry.getJSONObject(i)
                        todo["id"] = obj.getInt("id")
                        todos.add(todo)

                        val view = RemoteViews(context.packageName, R.layout.each_todo).apply {
                            setTextViewText(R.id.each_todo_container_text, todo["taskName"].toString())

                            val fillInIntent = Intent().apply {
                                Bundle().also { extras ->
                                    extras.putInt("todo_id", todo["id"].toString().toInt())//this isn't working for some reason
                                    putExtras(extras)
                                }
                            }
                            Log.d("debugging",  "id received is ${todo["id"].toString()}" )
                            setOnClickFillInIntent(R.id.each_todo_container_text, fillInIntent)
                        }

                        todosRemoteView.addItem(todo["id"].toString().toInt().toLong(), view)
                    }
                }

                setRemoteAdapter(
                        R.id.todos_list,
                        todosRemoteView
                    .build()
                )

                val pendingIntentx: PendingIntent = Intent(
                        context,
                        WidgetProvider::class.java
                ).run {
                    PendingIntent.getBroadcast(context, 0, this, PendingIntent.FLAG_IMMUTABLE or Intent.FILL_IN_COMPONENT)
                }
                setPendingIntentTemplate(R.id.todos_list, pendingIntentx)
            }

            appWidgetManager.updateAppWidget(widgetId, views)
    }

    }

    override fun onReceive(context: Context?, intent: Intent?) {
        val viewIndex: Int = intent!!.getIntExtra("todo_id", 0)
        Log.d("debugging", "an item is clicked $viewIndex")
        //the default value gets used, which means that the receiver isn't receiving the intent data
        
        super.onReceive(context, intent)
    }
}

问题是接收器没有接收到放入 fillInIntent 的数据,默认值 0 被最后一条语句打印出来。我还尝试按照 here 的建议设置 FILL_IN_COMPONENT 标志,但这没有用。我哪里错了?

android android-widget
1个回答
0
投票

只需将

PendingIntent.FLAG_IMMUTABLE or Intent.FILL_IN_COMPONENT
替换为
PendingIntent.FLAG_MUTABLE
。我认为是
IMMUTABLE
使得意图无法改变。结果,你不能得到额外的。

© www.soinside.com 2019 - 2024. All rights reserved.