android - how to make the update widget service on period -
i tried write code make update widget(notifications) if new item exist in rss feed ( xml file ) since application news of university , every thing done ok expect update done on click not on peroid of time !
snapshot update widget :
update service code ( update on click ) :
package com.example.testfeeds; import java.io.ioexception; import java.io.inputstream; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.random; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory; import android.annotation.suppresslint; import android.app.pendingintent; import android.app.service; import android.appwidget.appwidgetmanager; import android.content.componentname; import android.content.intent; import android.content.sharedpreferences; import android.graphics.color; import android.os.ibinder; import android.util.log; import android.webkit.webview.findlistener; import android.widget.arrayadapter; import android.widget.listview; import android.widget.remoteviews; import android.widget.textview; public class updatewidgetservice extends service { private static final string log = "testwidgets"; int numberofitems=0; arraylist<string> feedspubdate; @suppresswarnings("deprecation") @override public void onstart(intent intent, int startid) { log.i(log, "called"); // create random data feedspubdate=new arraylist<string>(); appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(this .getapplicationcontext()); int[] allwidgetids = intent .getintarrayextra(appwidgetmanager.extra_appwidget_ids); componentname thiswidget = new componentname(getapplicationcontext(), mywidgetprovider.class); int[] allwidgetids2 = appwidgetmanager.getappwidgetids(thiswidget); log.w(log, "from intent" + string.valueof(allwidgetids.length)); log.w(log, "direct" + string.valueof(allwidgetids2.length)); (int widgetid : allwidgetids) { /////////////////////////////////////////////////////////////////////////// remoteviews remoteviews = new remoteviews(this .getapplicationcontext().getpackagename(), r.layout.widget_layout); try { numberofitems=dotestfeed(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } // set text remoteviews.settextcolor(r.id.textview1,color.white); remoteviews.settextviewtext(r.id.textview1," "+ string.valueof(numberofitems)); log.w(log, string.valueof(numberofitems)); //////////////////////////////////////////////////////////////////////////// // register onclicklistener intent clickintent = new intent(this.getapplicationcontext(), mywidgetprovider.class); clickintent.setaction(appwidgetmanager.action_appwidget_update); clickintent.putextra(appwidgetmanager.extra_appwidget_ids, allwidgetids); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), 0, clickintent, pendingintent.flag_update_current); remoteviews.setonclickpendingintent(r.id.textview1, pendingintent); appwidgetmanager.updateappwidget(widgetid, remoteviews); } stopself(); super.onstart(intent, startid); } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } int dotestfeed() throws malformedurlexception, parseexception { log.d("msg"," in test feed"); inputstream = null; int x = 0; url myurl = new url("http://yunn.yu.edu.jo/index.php?option=com_content&view=category&id=55&layout=blog&itemid=104&format=feed&type=rss"); try { urlconnection conn = myurl.openconnection(); = conn.getinputstream(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } xmlpullparserfactory pullparserfactory; try { pullparserfactory = xmlpullparserfactory.newinstance(); xmlpullparser parser = pullparserfactory.newpullparser(); parser.setfeature(xmlpullparser.feature_process_namespaces, false); parser.setinput(is, null); log.d("msg","before making parsing"); x=parsexml(parser); log.d("msg","after making parsing"); } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } log.d("msg"," done testing"); return x; } ////////////////////////////////////////////////////////////////////////////////// @suppresslint("simpledateformat") private int parsexml(xmlpullparser parser) throws xmlpullparserexception,ioexception, parseexception { log.d("msg"," in parser"); int eventtype = parser.geteventtype(); int getelement=0; string pubdate=null; int numberofitems=0; while (eventtype != xmlpullparser.end_document){ string tagname = null; switch (eventtype){ //----------------------------------// case xmlpullparser.start_document: { // nothing } break; //----------------------------------// case xmlpullparser.start_tag: { tagname = parser.getname(); if ("item".equals(tagname)){ getelement=1; } else if (getelement!=0){ if ("pubdate".equals(tagname)){ pubdate= parser.nexttext(); //string parsedpubdate [] = pubdate.split(" "); feedspubdate.add(pubdate); log.d("value",pubdate); } } } break; //----------------------------------// case xmlpullparser.end_tag: { tagname = parser.getname(); if (tagname.equalsignorecase("item") && getelement != 0){ } } break; //----------------------------------// }// end-switch. eventtype= parser.next(); }// end-while. int i=0; sharedpreferences sp = getsharedpreferences("tempdata", 0); string datestringa=sp.getstring("recentpubdate", null); log.d("oldest date",datestringa); for(string s : feedspubdate ) { string datestringb = feedspubdate.get(i); simpledateformat parsersdf = new simpledateformat("eee, dd mmm yyyy hh:mm:ss"); date datea = parsersdf.parse(datestringa); date dateb = parsersdf.parse(datestringb); if (datea.compareto(dateb) < 0) { log.d("imp msg","one new item"); numberofitems++; } i++; } log.d("update result", string.valueof(numberofitems)); // toast.maketext(generalnews.this,"the size of list"+feedstitles.size() , toast.length_long).show(); return numberofitems; } //end xmlparser method. ////////////////////////////////////////////////////////////////////////////////// }
the widget provider code :
package com.example.testfeeds; import android.os.bundle; import android.app.activity; import android.view.menu; import java.util.random; import android.app.pendingintent; import android.appwidget.appwidgetmanager; import android.appwidget.appwidgetprovider; import android.content.componentname; import android.content.context; import android.content.intent; import android.util.log; import android.widget.remoteviews; public class mywidgetprovider extends appwidgetprovider { private static final string log = "testwidgets"; @override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { log.w(log, "onupdate method called"); // ids componentname thiswidget = new componentname(context, mywidgetprovider.class); int[] allwidgetids = appwidgetmanager.getappwidgetids(thiswidget); // build intent call service intent intent = new intent(context.getapplicationcontext(), updatewidgetservice.class); intent.putextra(appwidgetmanager.extra_appwidget_ids, allwidgetids); // update widgets via service context.startservice(intent); } }
you should create widget provider xml file , add following inside:
android:updateperiodmillis="20000"