0

I would like pass an value to my widget using Intent. But something wrong, I can not receive my intent extra value..

My MainActivity:

 var testValue = "test"
        val intent = Intent("my.action.string")
        intent.putExtra("extra", testValue)
        sendBroadcast(intent)

Manifest:

     <receiver
                android:name=".ui.widget.RateWidget"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                    <action android:name="my.action.string" />
                </intent-filter>
    
                <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/rate_widget_info" />
</receiver>

My Widget class:

class RateWidget: AppWidgetProvider() {

    private var myMessage = "default"

    override fun onReceive(context: Context?, intent: Intent?) {
        super.onReceive(context, intent)

        val action = intent?.action

        if (action == "my.action.string") {
            val state = intent.extras!!.getString("extra")
            if (state != null) {
                myMessage = state
            }
        }
    }

1 Answers1

0

Try it in widget:

val state = intent.getStringExtra("extra")
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '23 at 07:58