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.