Я изучаю Android последние 3 месяца или около того. Впрочем, ничего подобного я еще не встречал.
Я хочу получить доступ к нескольким различным веб-службам при первоначальной загрузке приложения. Ответ от этих веб-сервисов должен идти в БД для поиска, где это необходимо в приложении. У меня есть заставка, с которой я пытаюсь сделать это:
public class SplashScreen extends BaseActivity {
protected static final int SPLASH_DURATION = 2000;
protected ContactInfoRetriever contactInfoRetriever = new ContactInfoRetriever();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
startSplashThread();
}
private void startSplashThread() {
Thread splashThread = new Thread() {
@Override
public void run() {
try {
Looper.prepare();
// fire off the calls to the different web services.
updateContactInfo();
updateFooInfo();
updateBarInfo();
int waited = 0;
while (waited < SPLASH_DURATION) {
sleep(100);
waited += 100;
}
}
catch (InterruptedException e) {
Log.e(SplashScreen.class.getSimpleName(), "The splash thread was interrupted.");
}
finally {
finish();
startActivity(new Intent(SplashScreen.this, LandingPageActivity.class));
}
}
};
splashThread.start();
}
protected void updateContactInfo() {
PerformContactInfoSearchTask task = new PerformContactInfoSearchTask();
task.execute();
}
protected void updateFooInfo() {
PerformFooTask task = new PerformFooTask();
task.execute();
}
protected void updateBarInfo() {
PerformBarTask task = new PerformBarTask();
task.execute();
}
private class PerformContactInfoSearchTask extends AsyncTask<String, Void, ContactInfo> {
@Override
protected ContactInfo doInBackground(String... params) {
// this calls a class which calls a web service, and is then passed to an XML parser.
// the result is a ContactInfo object
return contactInfoRetriever.retrieve();
}
@Override
protected void onPostExecute(final ContactInfo result) {
runOnUiThread(new Runnable() {
public void run() {
InsuranceDB db = new InsuranceDB(SplashScreen.this);
// insert the ContactInfo into the DB
db.insertContactInfo(result);
}
});
}
}
private class PerformFooTask extends AsyncTask<String, Void, FooInfo> {
// similar to the PerformContactInfoSearchTask
}
private class PerformBarTask extends AsyncTask<String, Void, BarInfo> {
// similar to the PerformContactInfoSearchTask
}
}
У меня пока не получилось. Каков наилучший способ сделать это? Мне не нужно обновлять поток пользовательского интерфейса, когда задачи выполнены. Значит ли это, что я должен использовать что-то кроме AsyncTask
? Я читал кое-что о Лупере и Хэндлере. Это правильная вещь для использования? Любые примеры кода были бы замечательными.
Спасибо,
Zack