Question

I've developed a bit in Android before but have never encountered Widgets, until now, and they seem to have some restrictions in whats possible to do with them. So I'm asking for some guidance in how to do things right.

I have ecountered two problems which I don't know how to solve, and Google around on them doesn't give me a united answer.

Problem 1) I have som shared preferences in my application which I would like to be able to set from my widget(toggle function on button). But the only onclicklistener I can attach to the widgets remoteViews are setOnClickPendingIntent() and setOnClickFillInIntent(), I would like to run some methods that does the shared preferences handling when I click on a button. But if I understand this right you can only start a pending intent on click, is it really this restricted? I would like to run some method onclick instead, is this even possible to do and if it is, how is this done best? Also I would like that the widget is updated when user press the button.

Problem 2) Is it possible to update a widget from an activity, and whats the way to go?

NB. I'm not looking for any complete solutions, just best practice tips, hints, small examples or links.

Was it helpful?

Solution

Problem 1: Have a look at the example in the App Widget overview, you can execute code within the AppWidgetProvider, started by an intent. You must use intents, as you must switch to another user id / process, otherwise you could use another app's permissions (and/or vice versa).

Problem 2: You can update an Widget by its id, see the updating from the configuration activity section of the App Widget overview.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top