How to run a function background in android? -
In my application I have a function to fill the list visit. I want to run this function periodically every 5 minutes. I have used the following method for that.
Private class fresh thread thread {public boolean = location = false; Public void run () {try {while {isl}} {headlines.this.runOnUiThread (new runnable) {public void run () {populate_listview ()} function to populate // listview}}); Try {Thread.sleep (300000); } Hold (exception before) {}}} hold (exception e) {}}} When I use this method, the function runs on the foreground, so the whole application Is affected by the I want to run this function in the background, so that the list view updates and the user does not know that the function is running.
The function is to populate the following list view.
Public Zero populate_listview () {ArrayList & lt; Hashmap & lt; String, string & gt; & Gt; NewsList = new array list & lieutenant; Hashmap & lt; String, string & gt; & Gt; (); XMLParser parser = new XMLParser (); String xml = parser.getXmlFromUrl (url); // Getting URL from document doc = parser.getDomElement (xml); // Getting dom element node list nl = doc.getElementsByTagName (KEY_HEAD); // Looping through all song nodes & lt; Song & gt; Nodelist itemlist = doctor.gatealementbugname ("item"); String MarqueeStr = ""; For (Int i = 0; I & lt; nl.getLength (); i ++) {// Creating a new Hashmap hashmap & lt; String, string & gt; Map = new hashmap & lt; String, string & gt; (); Element E = (element) nl.item (i); //map.put(KEY_DATE, parser.getValue (e, KEY_DATE)); NewsList.add (map); }
To get a small display overhead, all you need to do is prepare code> With a background with a proper background, leave it in your background thread and in populate_listview () method only newList.add (refresh_thread.getMap ()); Hasham from another class to add it to just refreshthreadthread synchronize method getMap () And the second synchronize method readyHashMap () to prepare the code, obviously, your hashmap should be a field of RefreshThread class After this, the run () method will look like this: running public zeros () {try {while (IsStop) {readyHashMap}; Headlines.this.runOnUiThread (New Runnabel) {Public void run ()} {populate_listview (); // work to populate listview}}}); Try {Thread.sleep (300000); } Hold (Exception Ex) {}}} Grip (Exception E) {}}
Comments
Post a Comment