Button click event for android widget

后端 未结 5 1163
醉酒成梦
醉酒成梦 2021-01-30 13:16

I have an android widget that fetches data from a server every 10 minutes and display\'s it on the screen.
I\'d like to add a \"Refresh\" button to that widget.
When th

5条回答
  •  情深已故
    2021-01-30 13:32

    I tried the solution suggested by Sharon Haim Pour above, but my onReceive() method in AppWidgetProvider class has never been called on button press.

    Intent intent = new Intent(WIDGET_BUTTON);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.MY_BUTTON_ID, pendingIntent );
    

    After some research I could resolve the problem by updating the code as below:

    Intent intent = new Intent(context, MY_APPWIDGETPROVIDER_CLASS.class);
    intent.setAction(WIDGET_BUTTON);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.MY_BUTTON_ID, pendingIntent );
    

    Do not forget to put below:

    appWidgetManager.updateAppWidget(appWidgetId, views);
    

提交回复
热议问题