saxparser计时器和asynctask

时间:2012-07-27 15:26:53

标签: android timer android-asynctask saxparser

我可能有一个愚蠢/奇怪的问题

我是android和java的新手,我需要一些建议来了解android的方法以及如何使用它们

我有一个看起来像这样的主要活动类

extends  Activity implements OnClickListener{

     private static final String TAG = "ServicesDemo";
     public String myimageURL;
     private EditText Lyrics;
    private ImageView AlbumPic;
    private Button play,  stop;
    private TextView Artist, Song, Album, News, Lyric;
    private UpdateTimeTask m_updateTime;
    private Handler m_handler;
    Parser data;
    /** The delay in milliseconds between updates. */
    private final int DELAY = 20000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Artist =(TextView)findViewById(R.id.tvArtist);
        Song  =(TextView)findViewById(R.id.tvSongTitle);
        Album =(TextView)findViewById(R.id.tvAlbum);
        play = (Button) findViewById(R.id.play);
        stop = (Button) findViewById(R.id.stop);
        Lyrics = (EditText) findViewById(R.id.tvLyrics);
        News = (TextView)findViewById(R.id.tvAnouncement);
        AlbumPic = (ImageView) findViewById(R.id.AlbumPic);



        play.setOnClickListener(this);
        stop.setOnClickListener(this);



        m_updateTime = new UpdateTimeTask();

        m_handler = new Handler();
        m_handler.post(m_updateTime);
    }

    private class UpdateTimeTask implements Runnable {
        public void run() {


            try {

                SAXParserFactory saxPF = SAXParserFactory.newInstance();
                SAXParser saxP = saxPF.newSAXParser();
                XMLReader xmlR = saxP.getXMLReader();


                URL url = new URL("http://www.mysite.com/AndroidTest.php"); 
                XMLHandler myXMLHandler = new XMLHandler();
                xmlR.setContentHandler(myXMLHandler);
                xmlR.parse(new InputSource(url.openStream()));

            } catch (Exception e) {
                System.out.println(e);
            }
            data = XMLHandler.data;



            for (int i = 0; i < data.getTitle().size(); i++) {

           Lyrics.setText(data.getLyric().get(i));
                 myimageURL = data.getPic().get(i);

            Song.setText("Title = "+data.getTitle().get(i));


            Artist.setText("Artist = "+data.getArtist().get(i));


            Album.setText("Album = "+data.getAlbum().get(i)); 






 }

 downloadFile(myimageURL );

}

Bitmap bmImg;
void downloadFile(String fileUrl) {
 URL myFileUrl = null;
 try {
    myFileUrl = new URL(fileUrl);
 } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
 }
 try {
    HttpURLConnection conn = (HttpURLConnection) myFileUrl
          .openConnection();
    conn.setDoInput(true);
    conn.connect();
    InputStream is = conn.getInputStream();

    bmImg = BitmapFactory.decodeStream(is);
    AlbumPic.setImageBitmap(bmImg);
 } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
 }
 m_handler.postDelayed(m_updateTime, DELAY);
 AlbumPic.clearAnimation();
}

}

extends Activity implements OnClickListener{ private static final String TAG = "ServicesDemo"; public String myimageURL; private EditText Lyrics; private ImageView AlbumPic; private Button play, stop; private TextView Artist, Song, Album, News, Lyric; private UpdateTimeTask m_updateTime; private Handler m_handler; Parser data; /** The delay in milliseconds between updates. */ private final int DELAY = 20000; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Artist =(TextView)findViewById(R.id.tvArtist); Song =(TextView)findViewById(R.id.tvSongTitle); Album =(TextView)findViewById(R.id.tvAlbum); play = (Button) findViewById(R.id.play); stop = (Button) findViewById(R.id.stop); Lyrics = (EditText) findViewById(R.id.tvLyrics); News = (TextView)findViewById(R.id.tvAnouncement); AlbumPic = (ImageView) findViewById(R.id.AlbumPic); play.setOnClickListener(this); stop.setOnClickListener(this); m_updateTime = new UpdateTimeTask(); m_handler = new Handler(); m_handler.post(m_updateTime); } private class UpdateTimeTask implements Runnable { public void run() { try { SAXParserFactory saxPF = SAXParserFactory.newInstance(); SAXParser saxP = saxPF.newSAXParser(); XMLReader xmlR = saxP.getXMLReader(); URL url = new URL("http://www.mysite.com/AndroidTest.php"); XMLHandler myXMLHandler = new XMLHandler(); xmlR.setContentHandler(myXMLHandler); xmlR.parse(new InputSource(url.openStream())); } catch (Exception e) { System.out.println(e); } data = XMLHandler.data; for (int i = 0; i < data.getTitle().size(); i++) { Lyrics.setText(data.getLyric().get(i)); myimageURL = data.getPic().get(i); Song.setText("Title = "+data.getTitle().get(i)); Artist.setText("Artist = "+data.getArtist().get(i)); Album.setText("Album = "+data.getAlbum().get(i)); } downloadFile(myimageURL ); } Bitmap bmImg; void downloadFile(String fileUrl) { URL myFileUrl = null; try { myFileUrl = new URL(fileUrl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bmImg = BitmapFactory.decodeStream(is); AlbumPic.setImageBitmap(bmImg); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } m_handler.postDelayed(m_updateTime, DELAY); AlbumPic.clearAnimation(); } }

代码工作正常

你可以看到我使用计时器和服务

服务代码写在另一个名为“my_service”的类中 计时器代码写在主类

我的问题是我应该把我现在在主类中的定时器代码放在一个单独的类中,例如“timer class”

然后从主类中调用它,就像我为服务

所做的那样

或者就像现在一样好吗

因为xml在互联网服务器上,我应该使用asynctask吗?

我是否理解asynctask只运行一次所以如果我使用asynctask我必须将它与计时器结合使用以保持UI更新

谢谢

P.S。是一个“线程”与“类”相同

1 个答案:

答案 0 :(得分:0)

除非你想要进行一些重大的重构,否则我将定时器代码留在那里。

这是因为您在UpdateTimeTask中访问了主要活动的许多私有成员。将其分解为单独的文件,您将无法再以当前状态访问这些成员。

如果你正在进行一些重构,你可以使AsyncTask工作。当您想要执行您正在执行的单个任务(而不是像轮询一样)时,可以使用它们。如果您发现AsyncTask导致问题,请使用handler与UI进行通信。

最后,线程与Java中的类不同。 class表示特定对象的一组特征和命令,而thread表示执行路径。类对象是用于执行操作的东西,而线程是某些操作集合。