【发布时间】:2014-05-15 21:56:30
【问题描述】:
我的启动画面同步我的应用程序:
当我使用时:
sd.execute("init_sync", null).get();
我的徽标(在 xml 中定义)消失了。如果我退出 .get(),它就会出现。
这是我的代码:
public class SplashScreen extends Activity {
private Context ctx = null;
private Usuario mUser = null;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ctx = this;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
new Handler().post(new Runnable() {
@Override
public void run() {
// Check if user exists
Gson gson = new Gson();
String jsonUser = prefs.getString("usuario", "");
mUser = gson.fromJson(jsonUser, Usuario.class);
if (NetworkUtils.isOnline(ctx)) {
if (mUser != null) {
SyncData sd = new SyncData(ctx);
try {
sd.execute("init_sync", null).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
Intent i = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(i);
}
} else {
if (mUser != null) {
Intent i = new Intent(SplashScreen.this, DashBoard.class);
startActivity(i);
} else {
Toast.makeText(ctx, "Necesita Internet para loguearse", Toast.LENGTH_LONG).show();
finish();
}
}
}
});
}
}
我有几个 asyncTask 用于上传图片,并将 MySQL 数据库与我的 SQLite 数据库同步。所以,我需要等到所有进程结束才能知道是否有任何错误。
问题是我把它放在一个线程中,这样它就不会影响 UI。我哪里错了?
【问题讨论】:
标签: android multithreading android-asynctask