1

I everyone I'm using CloudRail Android SDK to post on Facebook and Twitter from my Android App using this code:

final Social facebook = new Facebook(getContext(),"xxxxxx","xxxxxx");
new Thread() {
    @Override
    public void run() {
        Log.i("SHARE","Condivido: " + testoCondivisione);
        facebook.postUpdate(testoCondivisione);
    }
}.start();

And I can post my text but I want to display a Dialog when the app return on display. - Is there a callback or something like this to "catch" the return on display of the app? - Is there a way to intercept the success or the in-success of the postUpdate function?

2 Answers2

1

the CloudRail Android SDK is working synchronously so there are no callbacks. This is also why some form of Threads should be used as you already correctly do. You can simply put the code you want to execute after the post was successful behind that method call. Errors can be caught with a try-catch. For example:

final Social facebook = new Facebook(getContext(),"xxxxxx","xxxxxx");
new Thread() {
    @Override
    public void run() {
        Log.i("SHARE","Condivido: " + testoCondivisione);
        try {
            facebook.postUpdate(testoCondivisione);
        } catch (Exeption e) {
            // Handle potential errors
        }
        Log.i("SHARE", "Post successful"); // Will be executed after a successful post
    }
}.start();
Florian
  • 712
  • 5
  • 18
1

This is my final solution.

public void shareViaFacebook(String text){
    final String textSocial = text;
    final Handler handler = new Handler(Looper.getMainLooper());

    Log.i("SHARE","Facebook");
    CloudRail.setAppKey("xxxxx");
    final Social facebook = new Facebook(getContext(),"xxxxx","xxxx");
    new Thread() {
        @Override
        public void run() {
            Log.i("SHARE","Condivido: " + textSocial);
            Boolean isEccezione = false;
            try {
                facebook.postUpdate(textSocial);
            } catch (Exception e) {
                Log.i("ECCEZIONE-FACEBOOK",e.toString());
                isEccezione = true;
            }

            //Controllo se c'รจ stata una eccezione
            if(isEccezione){
                handler.postDelayed(new Runnable() {
                    public void run() {
                        displayMessaggioCondivisione(false);
                    }
                },10);
            } else {
                Log.i("SHARE", "Post successful"); // Will be executed after a successful post
                handler.postDelayed(new Runnable() {
                    public void run() {
                        displayMessaggioCondivisione(true);
                    }
                },10);
            }
        }
    }.start();
}

private void displayMessaggioCondivisione(Boolean resp){
    if(resp){
        //Print Toast or open dialog
        Toast.makeText(getContext(),R.string.condivisione_social_ok,Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getContext(),R.string.condivisione_social_ko,Toast.LENGTH_LONG).show();
    }
}

I used also a Handler because I want to display a toast as a callback to the user.