java - 2 buttons on widget - refresh and show activity -


i have widget 2 buttons button id refresh , second button id detailsinfo. first button should trigger widget update, second button show detailed info (downloaded after widget refreshed). weather widget. refresh should trigger download full weather data. basic weather info should displayed directly on widget, full data on details activity, launched on detailsinfo button click.

this code:

public class appwidget extends appwidgetprovider {      public static string action_details = "m.m.meteowidget.action_details";      @override     public void onreceive(context context, intent intent)     {         log.i("onreceive",intent.getaction());          super.onreceive(context, intent);     }      @override     public void onupdate(context ctxt, appwidgetmanager mgr, int[] appwidgetids)     {         componentname me = new componentname(ctxt, appwidget.class);         final remoteviews updateviews = new remoteviews(ctxt.getpackagename(), r.layout.widget_layout);          intent intent = new intent(ctxt, appwidget.class);         intent.setaction(appwidgetmanager.action_appwidget_update);         intent.putextra(appwidgetmanager.extra_appwidget_ids, appwidgetids);          pendingintent pi = pendingintent.getbroadcast(ctxt, 0, intent, pendingintent.flag_update_current);         updateviews.setonclickpendingintent(r.id.refresh, pi);           intent intent2 = new intent(ctxt, detailsactivity.class);         intent2.setaction(action_details);         pendingintent di = pendingintent.getactivity(ctxt, 0, intent2, 0);         updateviews.setonclickpendingintent(r.id.detailsinfo, di);           mgr.updateappwidget(me, updateviews);           (int = 0; < appwidgetids.length; i++)             new weatherinfo(updateviews,appwidgetids[i],mgr).execute();      } } 

weatherinfo class performs weather details download (it extends asynctask). can see, gets updateviews constructor argument , sets basic weather info displayed on widget.

however, have no idea how display detailed info activity , pass detailed weather info it. when try run activity shown above, widget fails load ("problems loading widget"), without exception can debug.

any ideas doing wrong?

[edit] seems (almost) ok:

widget provider:

 @override     public void onreceive(context context, intent intent)     {         log.i("onreceive",intent.getaction());          super.onreceive(context, intent);     }      @override     public void onupdate(context ctxt, appwidgetmanager mgr, int[] appwidgetids)     {         componentname me = new componentname(ctxt, appwidget.class);         final remoteviews updateviews = new remoteviews(ctxt.getpackagename(), r.layout.widget_layout);          intent intent = new intent(ctxt, appwidget.class);         intent.setaction(appwidgetmanager.action_appwidget_update);         intent.putextra(appwidgetmanager.extra_appwidget_ids, appwidgetids);          pendingintent pi = pendingintent.getbroadcast(ctxt, 0, intent, pendingintent.flag_update_current);         updateviews.setonclickpendingintent(r.id.refresh, pi);           mgr.updateappwidget(me, updateviews);           intent intent2 = new intent(ctxt, detailsactivity.class);         intent2.setaction(action_details);         pendingintent di = pendingintent.getactivity(ctxt, 0, intent2, 0);         updateviews.setonclickpendingintent(r.id.detailsinfo, di);         mgr.updateappwidget(me, updateviews);           (int = 0; < appwidgetids.length; i++)             new weatherinfo(updateviews,appwidgetids[i],mgr).execute();      } 

my async task:

public class weatherinfo extends asynctask<string, void, map> {     private remoteviews views;     private int widgetid;     private appwidgetmanager widgetmanager;     private detailsactivity detailsactivity;      public weatherinfo(remoteviews views, int appwidgetid, appwidgetmanager appwidgetmanager)     {         this.views = views;         this.widgetid = appwidgetid;         this.widgetmanager = appwidgetmanager;     }        @override     protected map doinbackground(string... strings)     {         document doc = null;         try         {             doc = jsoup.connect("http://meteo.uwb.edu.pl/").get();         }         catch (ioexception e)         {             log.e("","connection failed: " + e.getmessage());             return null;         }         elements tables = doc.select("td");         elements headers = tables.get(2).select("b");         elements vals = tables.get(3).select("b");         map = new linkedhashmap();          (int i=0;i<headers.size() ; i++)             all.put(headers.get(i).text(),vals.get(i).text());          global.weatherinfo = all;          return all;     }      @override     protected void onpostexecute(map map)     {         if(map==null) return;           string txt = "";           string temp = (string) map.values().toarray()[0];         string hum =  (string) map.values().toarray()[1];         string pressure =  (string) map.values().toarray()[2];         string temp2 =  "odczuwalna: " + map.values().toarray()[3];             views.settextviewtext(r.id.info_temp, temp);         views.settextviewtext(r.id.info_temp2, temp2);         views.settextviewtext(r.id.info_hum, hum);         views.settextviewtext(r.id.info_pressure, pressure);             widgetmanager.updateappwidget(widgetid, views);     }  } 

so there global class weatherinfo static field share value between thread , details activity.

however, there 2 things have no idea how fix: - if activity destroyed (removed last app list in android), after press details button on widget, activity empty (bacause global.weather info null). need trigger widget refresh again , lanunch activity - if try set global.weatherinfo inside postexecute method widgets fails show, without exception thrown - why? - tried trigger async task on create activity. created second weatherinfo constructor , passed detailsactivity object this, able refresh activity. if don't use second constructor, widgets again fails load without exception.

i'm confused, can tell me what's going on here? , how solve problem?

create asynctask separately. return values postexecute() method. handler give better solution handle response of asynktask. call asynctask before populating values in ui components. handle error case separately either dialogue box whatever wish. follow link clearance in handler


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -