7. Load data from file, Cancel intent message Delivery
One of the ways to solve the last one is to ask the following questions:
Synchronously writes the day's meteorological data to a file for widget invocation every time the program is fetch data, and every time the widget is updated, the data is loaded from the file.
The modified data is as follows:
Add a method to the parsing tool class:
Public Static throws jsonexception{ new Jsontokener (itemdata). NextValue (); return New Weatheritem (object); }
Add two methods to the Ask a price tool class:
Public Static voidSavewidgetdata (Context context,weatheritem Weatheritem)throwsjsonexception,ioexception{Try{OutputStream out=Context.openfileoutput (widgetdate,context.mode_private); Out.write (Weatheritem.tojson (). toString (). GetBytes ()); Out.close (); }Catch(Jsonexception |IOException E) {E.printstacktrace (); } } Public StaticWeatheritem Loadwidgetdata (Context context)throwsioexception,jsonexception{BufferedReader Reader; Try{InputStream in=Context.openfileinput (widgetdate); Reader=NewBufferedReader (NewInputStreamReader (in)); StringBuilder Builder=NewStringBuilder (); String Line; while(line = Reader.readline ())! =NULL) {builder.append (line); } log.i ("TAG", builder.tostring ()); returnParsetools.parseitem (builder.tostring ()); }Catch(IOException e) {e.printstacktrace (); } return NULL; }
Change the Fetch method as follows:
Private voidFetchdata () {requestqueue mqueue=Volley.newrequestqueue (Getapplicationcontext ()); Mqueue.add (NewJsonobjectrequest (Request.Method.GET, URL,NULL,NewResponse.listener<jsonobject>() {@Override Public voidonresponse (Jsonobject jsonobject) {//This method is only called when the server has a response Try{Mweatheritems=parsetools.getinstance (jsonobject.tostring (), Parsetools.request_raw); //saving data to a fileFiletools.savedata (Getapplicationcontext (), mweatheritems); Toast.maketext (Getapplicationcontext (),"Update Complete", Toast.length_short). Show (); Madapter=NewItemadapter (Mweatheritems); Listview.setadapter (Madapter); Filetools.savewidgetdata (Getapplicationcontext (), Mweatheritems.get (0)); } Catch(Jsonexception |IOException E) {E.printstacktrace (); } } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror volleyerror) {Toast.maketext (Getapplicationcontext (),"Get Failed", Toast.length_short). Show (); } })); Mqueue.start (); }
In the Widgetprovider, you can just modify it as follows:
@Override Public voidOnUpdate (context context, Appwidgetmanager Appwidgetmanager,int[] appwidgetids) {log.i (TAG,"OnUpdate Update"); Remoteviews RV; Try{Mitem=Filetools.loadwidgetdata (context); }Catch(IOException |jsonexception e) {E.printstacktrace (); } if(Mitem! =NULL) {RV=Newremoteviews (Context.getpackagename (), r.layout.widget_layout); Rv.settextviewtext (R.id.date,mitem.getdate ()); }Else{RV=Newremoteviews (Context.getpackagename (), r.layout.widget_void_layout); } for(intappwidgetid:appwidgetids) {Appwidgetmanager.updateappwidget (APPWIDGETID,RV); } Super. OnUpdate (Context, Appwidgetmanager, appwidgetids); }
There is still no way to solve the problem when the widget is added to the widget after the program is installed but not started, and the widget is not synchronized with the widgets that were added after the program started.
Another problem is how to implement automatic weather updates and update widgets in your program every day, and consider this in the next phase.
Implement a simple weather forecast application (7)