Application widget with bundle?
Asked Answered
L

3

1

Hey guys, I have application widget, and I want to send some data to the intent that is attached to PendingIntent, by clicking the widget. here's my code

final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
    int appWidgetId = appWidgetIds[i];
    Intent intent = new Intent(context, UpComingBDays.class);
    if(bdaysAmount != 0){
    Bundle bundle = new Bundle();
    bundle.putIntegerArrayList("WIDGETIDS", tempAllIDS);
    intent.putExtras(bundle);
    System.out.println("bund insertedddddddddddddd.....................");
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
        intent, 0);
    RemoteViews remoteView = new RemoteViews(context.getPackageName(),
        R.layout.widget_layout);
    remoteView.setTextViewText(R.id.widget_text, finalText4TextView);
    remoteView.setOnClickPendingIntent(R.id.WidgetImageButton, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);

I realize always "bund insertedddddddddd......" is printed on CatLog, but the intent's bundle is null.

what is incorrect? how can i send data by clicking on widget. plz dont offer to use services as my code does not have anything with it. many thanks.

Leviathan answered 11/1, 2011 at 22:15 Comment(0)
C
0

Try using FLAG_UPDATE_CURRENT when you create your PendingIntent.

Countrydance answered 12/1, 2011 at 0:49 Comment(1)
Thanks for answer CW. I didn't use your suggestion because I've changed the approach to this problem. Initially I was doing some calculation and Service calling from widget class but I changed that, putting all the logic in attached intent.Leviathan
A
0

Sorry for answering post after half a year :) For those, who will find this post when get this problem: I have found, that FLAG_UPDATE_CURRENT is not enough:

http://developer.android.com/reference/android/app/PendingIntent.html

This flag will not work if you need more then 1 widget with different data in intents bundles, because Intent will be overrwritten.

To fix this you will need to update intent data:

http://developer.android.com/guide/topics/appwidgets/index.html

// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
Allergic answered 28/9, 2011 at 17:6 Comment(1)
Did you need to add a <data /> element in the intent-filter (usally for a BroadcastReceiver) in the AndroidManifest?Churrigueresque
H
0

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")

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)
    }
}
Hughs answered 29/6, 2022 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.