android 带参数的requestPinAppWidget

oalqel3c  于 2023-05-21  发布在  Android
关注(0)|答案(1)|浏览(137)

我正在使用:

val bundle = Bundle()
    bundle.putString("id", item.id)
    appWidgetManager.requestPinAppWidget(provider, bundle, pendingIntent)

要固定小部件,请执行以下操作。我想传递带参数的bundle(某个id)
但是当配置活动启动时,捆绑包是空的

intent?.extras?.getString("id")

如何让它工作?

bfhwhh0e

bfhwhh0e1#

我没有找到自己的解决办法,这样我就设法解决了。

val appWidgetManager = AppWidgetManager.getInstance(requireContext())
            val provider = ComponentName(requireContext(), SimpleNotesWidget::class.java)
    
            if (!appWidgetManager.isRequestPinAppWidgetSupported) {
                showToast("Pin app widget is not supported")
                return
            }
    
            val pendingIntent = WidgetReceiver.getPendingIntent(requireContext(), listId)
            appWidgetManager.requestPinAppWidget(provider, Bundle(), pendingIntent)

接收方:

class WidgetReceiver : BroadcastReceiver() {
    
        companion object {
            const val WIDGET_NAME = "WIDGET_NAME"
    
            fun getPendingIntent(context: Context, checkListId: String): PendingIntent {
                val callbackIntent = Intent(context, WidgetReceiver::class.java)
                val bundle = Bundle()
                bundle.putString(WIDGET_NAME, checkListId)
                callbackIntent.putExtras(bundle)
                return PendingIntent.getBroadcast(
                    context,
                    checkListId.hashCode(),
                    callbackIntent,
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE
                    else PendingIntent.FLAG_UPDATE_CURRENT
                )
            }
        }
    
        override fun onReceive(context: Context?, intent: Intent?) {
            if (context == null || intent == null) {
                return
            }
    
            val widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
            if (widgetId == -1) {
                return
            }
    
            val listId = intent.getStringExtra(WIDGET_NAME)
            val widgetPreferences = WidgetPreferences(context)
            widgetPreferences.setWidgetValues(widgetId, listId!!)
            context.updateWidgets()
        }
    }

配置活动:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    widgetId = intent?.extras?.getInt(
        AppWidgetManager.EXTRA_APPWIDGET_ID,
        AppWidgetManager.INVALID_APPWIDGET_ID
    ) ?: AppWidgetManager.INVALID_APPWIDGET_ID

    if (widgetId == null) {
        finish()
    }

    lifecycleScope.launch {
        delay(10)
        val listId =
            WidgetPreferences(this).getWidgetListId(widgetId!!)
        if (listId != null && checkWidgetPinned(listId)) {
            configureWidget(widgetId!!, listId)
            Toast.makeText(
                this,
                getString(R.string.widget_pinned),
                Toast.LENGTH_SHORT
            ).show()
            finish()
        }
    }
}

private fun configureWidget(widgetId: Int, checklistId: String) {
        WidgetPreferences(this@WidgetChecklistPickerActivity).apply {
            setWidgetValues(widgetId, checklistId)
        }
        updateWidgets()
        val resultValue = Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId)
        setResult(Activity.RESULT_OK, resultValue)
        finish()
    }

相关问题