Skip to main content

Clickable widgets in android



The developer documentation has seemed to have failed me here. I can create a static widget without thinking, I can even create a widget like the analogue clock widget that will update itself, however, I can not for the life of me figure out how to create a widget that reacts to when a user clicks on it. Here is the best code sample that the developer documentation gives to what a widget activity should contain (the only other hint being the API demos, which only creates a static widget):







public class ExampleAppWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

final int N = appWidgetIds.length;



// Perform this loop procedure for each App Widget that belongs to this provider

for (int i=0; i<N; i++) {

int appWidgetId = appWidgetIds[i];



// Create an Intent to launch ExampleActivity

Intent intent = new Intent(context, ExampleActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);



// Get the layout for the App Widget and attach an on-click listener to the button

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);

views.setOnClickPendingIntent(R.id.button, pendingIntent);



// Tell the AppWidgetManager to perform an update on the current App Widget

appWidgetManager.updateAppWidget(appWidgetId, views);

}

}

}







from: The Android Developer Documentation's Widget Page





So, it looks like pending intent is called when the widget is clicked, which is based off of an intent (I'm not quite sure what the difference between an intent and a pending intent is), and the intent is for the ExampleActivity class. So I made my sample activity class a simple activity that when created, would create a mediaplayer object, and start it (it wouldn't ever release the object, so it would eventually crash, here is it's code:







@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);

mp.start();

}







However, when I added the widget to the home screen, and clicked on it, nothing played, in fact, nothing played when I set the update timer to just a few hundred milliseconds (in the appwidget provider xml file). Furthermore, I set break points and found out that not only was it never reaching the activity, but no break points I set would ever get triggered. (I still haven't figured out why that is), however, logcat seemed to indicate that the activity class file was being run.





So, is there anything I can do to get an appwidget to respond to a click? As the onClickPendingIntent() method is the closest I have found to a onClick type of method.





Thank you very much.


Comments

  1. First, add an instance variable with a constant.

    public static String YOUR_AWESOME_ACTION = "YourAwesomeAction";

    Then you need to add the action to the intent before you add the intent to the pending intent:

    Intent intent = new Intent(context, widget.class);
    intent.setAction(YOUR_AWESOME_ACTION);

    Next, override the onReceive method:

    @Override
    public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);


    And then respond to your button presses by querying the intent returned for your action within the onReceive method:

    if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {
    //do some really cool stuff here
    }


    And that should do it!

    ReplyDelete
  2. You can take a look at Wikitionary sample in SDK to see how they process click. They do the same as you do and it works.

    ReplyDelete
  3. I had exactly the same problem and the given solution from Brian515 didn't help.
    In my case the configuration activiy won't open unless one of the following:
    1. Wait several minutes (more than 30 minutes).
    2. Add another widget. The first one would not open configuration activiy on click but the rest would.

    BTW, the widget behaved this way with or without the suggested solution here and I could not see any evidence of the program stepping into onRecieve with the given Intent.

    ReplyDelete
  4. RemoteViews views = new RemoteViews(context.getPackageName(),
    R.layout.appwidget_provider_layout);


    This did not work for me either but I changed it to reference the layout file.

    RemoteViews views = new RemoteViews(context.getPackageName(),
    R.layout.nameofthexmllayoutfile);

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex