android - Widget configuration activity not updating the widget on re-configuration. Only when it's called for the first time -
i'm new @ programming android. have clickable text widget, configuration activity works fine first time, i.e. when widget first created.
i want able click on widget, open same configuration activity, change settings , have update widget new configuration. can't done. i'm using sharedpreferences, still can't done.
i've read related questions here, , other sources well, nada.
please have @ code below , me if can. appreciate it.
config class
package com.example.widget; import android.app.activity; import android.app.pendingintent; import android.appwidget.appwidgetmanager; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.button; import android.widget.remoteviews; import android.widget.spinner; public class config extends activity { private config context; private int widgetid ; appwidgetmanager manager; remoteviews views; spinner lang_spinner; string lang; button apply; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.config); setresult(result_canceled); // set fields context = this; manager = appwidgetmanager.getinstance(context); views = new remoteviews(context.getpackagename(), r.layout.main); // start preferences object sharedpreferences apppreferences = context.getsharedpreferences("widget_preferences", 0); final sharedpreferences.editor appeditor = apppreferences.edit(); // widget id bundle extras = getintent().getextras(); if (extras != null) { widgetid = extras.getint(appwidgetmanager.extra_appwidget_id, appwidgetmanager.invalid_appwidget_id); } // functionality apply language lang_spinner = (spinner) findviewbyid(r.id.spinnerlang); lang_spinner.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { lang = lang_spinner.getselecteditem().tostring(); if (lang.equals("espanol")){ main.widget_text = getresources().getstring(r.string.widget_text_es); appeditor.putstring("wt"+widgetid, getresources().getstring(r.string.widget_text_es)).commit(); } else if (lang.equals("english")) { main.widget_text = getresources().getstring(r.string.widget_text_en); appeditor.putstring("wt"+widgetid, getresources().getstring(r.string.widget_text_en)).commit(); } } @override public void onnothingselected(adapterview<?> arg0) { appeditor.putstring("wt"+widgetid, getresources().getstring(r.string.widget_text_en)).commit(); } }); // apply settings apply = (button) findviewbyid(r.id.apply); apply.setonclicklistener(new onclicklistener() { public void onclick(view v) { // send pending intent textview, able click reconfigure intent intent = new intent(context, config.class); pendingintent piconfig = pendingintent.getactivity(context, 0, intent, 0); views.setonclickpendingintent(r.id.widgettext, piconfig); // set text views.settextviewtext(r.id.widgettext, main.widget_text); // commit changes preferences object // appeditor.commit(); // update widget manager.updateappwidget(widgetid, views); // return ok intent result = new intent(); result.setaction(appwidgetmanager.action_appwidget_update); result.putextra(appwidgetmanager.extra_appwidget_id, widgetid); setresult(result_ok, result); finish(); } }); } } main class
package com.example.widget; import android.appwidget.appwidgetmanager; import android.appwidget.appwidgetprovider; import android.content.componentname; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.preference.preferencemanager; import android.widget.remoteviews; public class main extends appwidgetprovider { // text displayed on widget static string widget_text; int widgetid; remoteviews views; @override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { super.onupdate(context, appwidgetmanager, appwidgetids); (int = 0; < appwidgetids.length; i++) { widgetid = appwidgetids[i]; // text preferences sharedpreferences quotepreferences = preferencemanager.getdefaultsharedpreferences(context); widget_text = apppreferences.getstring("wt"+widgetid, "no text yet."); // update view views = new remoteviews(context.getpackagename(), r.layout.main); views.settextviewtext(r.id.widgettext, widget_text); appwidgetmanager.updateappwidget(widgetid, views); } } @override public void onreceive(context context, intent intent) { // todo auto-generated method stub super.onreceive(context, intent); } } currently onreceive void, i've tried below no avail.
@override public void onreceive(context context, intent intent) { // todo auto-generated method stub super.onreceive(context, intent); appwidgetmanager manager = appwidgetmanager.getinstance(context); componentname thiswidget = new componentname(context.getpackagename(), main.class.getname()); int [] appwidgets = manager.getappwidgetids(thiswidget); onupdate(context, manager, appwidgets); } manifest
<receiver android:name="com.example.widget.main"> <intent-filter> <action android:name="android.appwidget.action.appwidget_update"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget"/> </receiver> <activity android:name="com.example.widgetconfig" android:theme="@android:style/theme.dialog"> <intent-filter> <action android:name="android.appwidget.action.appwidget_configure"/> <action android:name="android.appwidget.action.appwidget_update"/> </intent-filter> </activity> thanks again
Comments
Post a Comment