开发者

How i can implement the Service

I develop an Android Project in which I have to parse the RSS feeds. So I parse this feeds over the internet at the time of startup of application. But my problem is when parsing is executing then program will wait for long time. So i want to implement the Service, which will do the processing is in background. How I can do this?

My code for XML parsing is:

try 
        {
            feeditemshelper = new FeedItemsHelper();
            RootElement root = new RootElement("rss");
            Element channelElement = root.getChild("channel");
            Element itemElement = channelElement.getChild("item");
            itemElement.getChild("title").setEndTextElementListener(new EndTextElementListener()
            {

                @Override
                public void end(String body)
                {
                    LHParser.feeditemshelper.setTitle(body);
                }
            });
            Element descElement = itemElement.getChild("description");
            descElement.getChild("image").setEndTextElementListener(
                    new EndTextElementListener()
                    {
                        public void end(String body) 
   开发者_JAVA百科                     {
                            LHParser.feeditemshelper.setImage(body);
                        }
                    });
            descElement.getChild("text").setEndTextElementListener(
                    new EndTextElementListener()
                    {
                        public void end(String body) 
                        {
                            dflag++;
                            LHParser.feeditemshelper.setDescription(body,dflag);
                        }
                    });
            itemElement.getChild("pubDate").setEndTextElementListener(new EndTextElementListener()
            {

                @Override
                public void end(String body)
                {
                    LHParser.feeditemshelper.setPubDate(body);
                }
            });
            try 
            {
                Xml.parse(
                        getInputStreamFromUrl("http://www.bjp.org/index.php?option=com_ninjarsssyndicator&feed_id=12&format=raw"),
                        Xml.Encoding.UTF_8, root.getContentHandler());
            }
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }

        catch (Exception e) 
        {
            e.printStackTrace();
        }


You can do this using an AsyncTask. There is a nice example on the Android Dev Site. Your parsing logic needs to be done in the doInBackground method.

It will not block the UI as doInBackGround is executed in a seperate background thread.

You can show/hide a progress dialog in the onPreExecute/onPostExecute methods. (The onPreExecute/onPostExecute methods are run on the UI thread so there you can interact with the UI and notifiy the user - if you wish - when the processing starts/finishes).


Devang. you can use AsyncTask for load data in background for get detail for it so go link which is given by ddewaele. and you want know about service then Click Here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜