android - Why does AppWidgetManager update Intent of all RemoteViews for Widgets? -
i have widget defined in android app maps specific lightswitch chosen configuration activity. each instance of widget can map different lightswitch , tapping widget toggles light on , off. here's code activity sets widget:
@override public boolean onoptionsitemselected(menuitem item) { if (item.getitemid() != r.id.action_choose) { return super.onoptionsitemselected(item); remoteviews remoteviews = new remoteviews(this.getpackagename(), r.layout.device_widget); remoteviews.setimageviewresource(r.id.widgetonoffbutton, android.r.drawable.star_big_off); sharedpreferences.editor prefs = getapplicationcontext().getsharedpreferences(mywidgetservice.widget_prefs, mode_private).edit(); prefs.putint(mywidgetservice.widget_device_id + integer.tostring(mappwidgetid), mdeviceid); prefs.apply(); intent clickintent = new intent(this.getapplicationcontext(), mywidgetprovider.class); clickintent.setaction(appwidgetmanager.action_appwidget_update); clickintent.putextra(appwidgetmanager.extra_appwidget_id, mappwidgetid); clickintent.putextra(mywidgetservice.on_level, 100); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, clickintent, pendingintent.flag_update_current); remoteviews.setonclickpendingintent(r.id.widgetonoffbutton, pendingintent); mappwidgetmanager.updateappwidget(mappwidgetid, remoteviews); }
i've verified mappwidgetid new id widget. here's appwidgetprovider's code, use onreceive method can access extras in intent, have onupdate , ondelete defined intents fall outside normal click conditions:
@override public void onreceive(context context, intent intent) { string action = intent.getaction(); int widgetid = intent.getintextra(appwidgetmanager.extra_appwidget_id, -1); int onlevel = intent.getintextra(mywidgetservice.on_level, -1); if(action == appwidgetmanager.action_appwidget_update && widgetid > -1 && onlevel > -1) { intent newintent = new intent(context.getapplicationcontext(), wishfullwidgetservice.class); newintent.putextra(appwidgetmanager.extra_appwidget_id, widgetid); newintent.putextra(mywidgetservice.on_level, onlevel); context.startservice(newintent); } super.onreceive(context, intent); intent resultvalue = new intent(); resultvalue.putextra(appwidgetmanager.extra_appwidget_id, mappwidgetid); setresult(result_ok, resultvalue); finish(); return true; }
and code mywidgetservice. each time widget clicked, reset onclickpendingintent own widgetid , onlevel, widgets fire off pendingintent latest widget's id , onlevel.
@override public int onstartcommand(intent intent, int flags, int startid) { appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(this.getapplicationcontext()); int widgetid = intent.getintextra(appwidgetmanager.extra_appwidget_id, -1); int onlevel = intent.getintextra(on_level, -1); remoteviews remoteviews = new remoteviews(this.getapplicationcontext().getpackagename(), r.layout.device_widget); intent clickintent = new intent(this.getapplicationcontext(), wishfullwidgetprovider.class); clickintent.setaction(appwidgetmanager.action_appwidget_update); clickintent.putextra(appwidgetmanager.extra_appwidget_id, widgetid); if(onlevel > 0) { remoteviews.setimageviewresource(r.id.widgetonoffbutton, android.r.drawable.star_big_on); clickintent.putextra(on_level, 0); } else { remoteviews.setimageviewresource(r.id.widgetonoffbutton, android.r.drawable.star_big_off); clickintent.putextra(on_level, 100); } if(onlevel > -1) { int deviceid = mprefs.getint(widget_device_id + integer.tostring(widgetid), -1); sendmessagetodevice(onlevel, deviceid); } pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, clickintent, pendingintent.flag_update_current); remoteviews.setonclickpendingintent(r.id.widgetonoffbutton, pendingintent); appwidgetmanager.updateappwidget(widgetid, remoteviews); stopself(); return super.onstartcommand(intent, flags, startid); }
at point, if have widget 1 , widget 2, clicking widget 1 fire intent containing id , onlevel of widget 2. how can update onclickpendingintent of each widget independently without newest 1 overwriting others?
pass unique int first flag (second parameter) of pendingintent, preferably widgetid.
pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), uniqueid, clickintent, pendingintent.flag_update_current);
this cause android not override existing pendingintent, create new one.
here's link different post similar problem.
Comments
Post a Comment