Retrieving intent extras from widget
Asked Answered
M

2

5

I'm making a widget for my WebView app, and it's got a list of buttons on it. Currently, It's firing an intent whenever their pressed. In that intent, I'm putting some string extra's, but when the onNewIntent receives the intent, the value for the extra is NULL. So I'm stuck on receiving the actual string extra.

Here's the code on my list provider:

override fun getViewAt(positionIndexNum: Int): RemoteViews {

    ........

    val extrasObj = Bundle()
    extrasObj.putString("shortcutUrl", listViewUrlArr[positionIndexNum]) // I've tried hardcoding this part and it still returns null.
    extrasObj.putString("shortcutJs", listViewJsArr[positionIndexNum])
    extrasObj.putString("shortcutId", listViewIdArr[positionIndexNum])
    val fillInIntentObj = Intent()
    fillInIntentObj.putExtras(extrasObj)
    viewObj.setOnClickFillInIntent(listViewItemId, fillInIntentObj)
    return viewObj
}

Here's the code from the onNewIntent function:

override fun onNewIntent(intentObj: Intent) {
    super.onNewIntent(intentObj)
    val bundle = intentObj.extras
    if (bundle != null) {
        for (key in bundle.keySet()) {
            Log.e("TAG", key + " : " + if (bundle[key] != null) bundle[key] else "NULL")
        }
    }
    .....
}

That outputs in the logcat:

shortcutUrl : NULL
shortcutId : NULL
shortcutJs : NULL

I've also tried: intentObj.getStringExtra("shortcutId") which still returns NULL

EDIT:

I also have this PendingIntent code in the updateAppWidget function:

    val clickIntent = Intent(contextObj, MainActivity::class.java)

    val clickPI = PendingIntent.getActivity(contextObj, 0,
    clickIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT);

    viewsObj.setPendingIntentTemplate(R.id.widget_list, clickPI)
Metaphor answered 8/4, 2022 at 10:38 Comment(0)
M
19

I finally found a fix for this, I'm not sure how it really works but I changed:

PendingIntent.FLAG_IMMUTABLE

to

PendingIntent.FLAG_MUTABLE

in the PendingIntent. Hopefully this helps someone else!

Metaphor answered 11/4, 2022 at 13:56 Comment(0)
L
0

Here is a full answer which I posted on another question: Application widget with bundle?

Read the Intent in MyActivity:

private fun readIntent() {
    val intentExtras: Bundle? = intent.extras
    intentExtras?.let {
        val intentMessage: String? = intentExtras.getString(APPWIDGET_INTENT_MESSAGE)
        println(intentMessage)
    }
}

Here is a method in Kotlin to get pending intent to open your activity

fun getPendingIntentMyActivity(context: Context, message: String): PendingIntent {
    val intent = Intent(context, MyActivity::class.java)
    intent.action = APPWIDGET_INTENT
    intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))

    val extras = Bundle().apply {
        putString(APPWIDGET_INTENT, APPWIDGET_OPEN_APP)
        putString(APPWIDGET_INTENT_MESSAGE, message)
    }
    intent.putExtras(extras)
    return PendingIntent.getActivity(context, 0, intent, FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
}

Then set it in the Widget

remoteViews.setOnClickPendingIntent(R.id.rootView, getPendingIntentMyActivity(context, "Hello World")
Lowtension answered 29/6, 2022 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.