Вопрос

My splash screen syncronize my app :

When I use :

sd.execute("init_sync", null).get();

My logo (defined in xml) disappear. If I quit .get(), it appears.

Here is my code :

 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();
                }
            }
        }
    });

}

}

I have several asyncTask that I use to upload pics, and sync MySQL database with my SQLite database. So, I need to wait till all the processes end to know if there is any error.

The thing is I put it in a thread, so that it would not affect UI. Where am I wrong?

Это было полезно?

Решение 2

Like @ashishduh says, I was in UI Thread. So I changed:

new Handler().post(new Runnable() {
@Override
        public void run() {
....
     }
}

by

Runnable sync = new Runnable() {
        @Override
        public void run() {
 ....
     }
};

Thread t = new Thread(sync);
t.start();

And it solved my problem!

Другие советы

When you use get() it causes the UI thread to wait. Don't use get(). You need to override the onPostExecute method in AsyncTask.

private Boolean task1Finished = false;
private Boolean task2Finished = false;
private Boolean task3Finished = false;

//...

SyncData sd1 = new SyncData(ctx) {
    @Override
    protected void onPostExecute(Object result) {
        task1Finished = true;
        goToNextActivity();
    }
};

SyncData sd2 = new SyncData(ctx) {
    @Override
    protected void onPostExecute(Object result) {
        task2Finished = true;
        goToNextActivity();
    }
};

SyncData sd3 = new SyncData(ctx) {
    @Override
    protected void onPostExecute(Object result) {
        task3Finished = true;
        goToNextActivity();
    }
};

try {
    sd1.execute();
    sd2.execute();
    sd3.execute();
} 
catch (InterruptedException e) {
    e.printStackTrace();
} 
catch (ExecutionException e) {
    e.printStackTrace();
}

//...

private void goToNextActivity() {
    if (task1Finished && task2Finished && task3Finished)
        // all tasks complete
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top