Implement a simple weather forecast application (7)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.