Widget update via remoteview Android -
i want activity "but1" update text on widget button through button in but1 activity. i've tried , shows no error still widget doesn't updates. im missing in manifest file?moreover log cat doesnt show problem following code in but1.class
et1.setonclicklistener(new onclicklistener() { intent in=new intent(); bundle extras=in.getextras(); if(extras!=null) { awid=extras.getint(appwidgetmanager.extra_appwidget_id,appwidgetmanager.invalid_appwidget_id); } aw=appwidgetmanager.getinstance(context); @override public void onclick(view arg0) { // todo auto-generated method stub remoteviews rv=new remoteviews(context.getpackagename(),r.layout.activity_main); rv.settextviewtext(r.id.button1, "mughazf,f,jvnl"); aw.updateappwidget(awid, rv); intent result=new intent(); result.putextra(appwidgetmanager.extra_appwidget_id, awid); setresult(result_ok,r esult);
and widget is:
@override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { // todo auto-generated method stub super.onupdate(context, appwidgetmanager, appwidgetids); final int n = appwidgetids.length; (int i=0; i<n; i++) { int appwidgetid = appwidgetids[i]; intent intent = new intent(context, but1.class); pendingintent pendingintent = pendingintent.getactivity(context, 0, intent, 0); remoteviews views = new remoteviews(context.getpackagename(), r.layout.activity_main); views.setonclickpendingintent(r.id.button2, pendingintent); appwidgetmanager.updateappwidget(appwidgetid, views); } } }
problem should widget app id
. need keep remembering widget id
update later.
your widget provider
onupdate() method should store widget id
in shared preference
or database
i using shared preference
here,
sharedpreferences prefs = context.getsharedpreferences("private preference", context.mode_private); // here saving 1 widget id. change logic store widget id's prefs.edit().putint("widgetid",appwidgetid).commit();
then when activity
launched , button clicked. can update widget layout
this
first read widget id
shared preference
sharedpreferences prefs = getsharedpreferences("private preference", context.mode_private); int awid = prefs.getint("widgetid", appwidgetmanager.invalid_appwidget_id);
then, create instance of remoteview
, set new text textview
remoteviews rv=new remoteviews(getpackagename(),r.layout.widget_layout); rv.settextviewtext(r.id.textview1, "some 1 updated me");
then , instance of appwidget manager
appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(getapplicationcontext());
and, update widget
new remoteview
layout
.
appwidgetmanager.updateappwidget(awid, rv);
so, awid - widget id
got onupdate
method.
this should work. let me know if see issue.
Comments
Post a Comment