i have maybe a stupid/strange question
i am new to android and java and i need some advice to understand the methods of android and how to use them
i have a main activity class that looks like this
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();
}
}
the code works ok
as you can see i use a timer and a service
the service code is written in a different class called "my_service" the timer code is written in the main class
my question is should i put the timer code which i have now in the main class in a separate class for example "timer class"
and then call it from the main class just like i did for the for the service
or is it ok just like it is now
and because the xml is on a internet server should i use asynctask?
did i understood it right that the asynctask only runs once so if i use a asynctask i have to use it in combination with a timer to keep the UI updated
thanks
p.s. is a "thread" the same as a "class"