您可以查看以下链接
http://developer.android.com/reference/android/app/Service.html
http://developer.android.com/guide/components/services.html
http://www.vogella。 com/articles/AndroidServices/article.html
您可以添加一个线程,该线程将在每 5 秒后下载一次代码。这是代码
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
String tag="TestService";
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
Log.i(tag, "Service created...");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(tag, "Service started...");
fetchData();
}
public void fetchData()
{
new Thread(new Runnable()
{
@Override
public void run()
{
while(true)
{
final String strRes = downloadXML("http://uri.com");
runOnUiThread(new Runnable()
{
@Override
public void run()
{
//do whatever once download complete
}
});
try{
Thread.sleep(5000);
}catch(Exception ee){}
}
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}