Button click event for android widget

后端 未结 5 1125
醉酒成梦
醉酒成梦 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:51

    I found out how to do that.
    Add an action to the AndroidManifest.xml file in the > tag:

    
    

    In the provider add a constant that matches the action name:

    public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON";
    

    In the onUpdate() method add a pending intent that matches the action:

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

    Finally, in the onRecieve() method, check the action name:

     if (WIDGET_BUTTON.equals(intent.getAction())) {
    //your code here
    
        }
    

提交回复
热议问题