Two buttons with PendingIntents - Widget

元气小坏坏 提交于 2020-01-13 10:35:52

问题


I'm creating a widget with two buttons. One of them updates the content of the widget and the second one must launch an activity.

I have two PendingIntent for each action, but I can't make them both work. If one works the other one doesn't.

I've revised the code and can't understand what's wrong.

Any help will be very appreciated.

This is the code.

    RemoteViews controls = new RemoteViews(context.getPackageName(), R.layout.miwidget);

    Intent intent = new Intent("actony.com.ACTUALIZAR_WIDGET");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);


    Intent intentSettings = new Intent();  
    intentSettings.setClass(context,WidgetConfig.class);  


    PendingIntent pendingIntentUpdate = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    controls.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntentUpdate);

    PendingIntent pendingIntentSettings =  PendingIntent.getActivity(context, 0, intentSettings, 0);
    controls.setOnClickPendingIntent(R.id.botonSettings, pendingIntentSettings);

回答1:


Try adding the getActivity PendingIntent.FLAG_UPDATE_CURRENT aswell...

 PendingIntent pendingIntentSettings =  
      PendingIntent.getActivity(context, 0, intentSettings, PendingIntent.FLAG_UPDATE_CURRENT);

and if multiple widget's are possible add the widgetId there too.

Make sure both of the activities/broadcasts are listed in the manifest file.

Moreover, try creating the Intent with this constructor:

 Intent intent = new Intent(context,ACTUALIZAR_WIDGET.class);
 Intent intentSettings = new Intent(context,WidgetConfig.class);

add imports if needed.

Hope some of that will make you widget work.




回答2:


Check this link to know which button has been clicked when there is two or more button in a widget..

https://stackoverflow.com/a/10733049/1331593

It should work... IF it does not work please let me know what is the problem...



来源:https://stackoverflow.com/questions/7933669/two-buttons-with-pendingintents-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!