在我的安卓应用中,我使用CloudRail Android SDK在脸书和推特上发布了以下代码:
final Social facebook = new Facebook(getContext(),"xxxxxx","xxxxxx");
new Thread() {
@Override
public void run() {
Log.i("SHARE","Condivido: " + testoCondivisione);
facebook.postUpdate(testoCondivisione);
}
}.start();我可以发布我的文本,但我想在应用程序返回显示时显示一个对话框。-有一个回调或类似的东西来“捕捉”应用程序显示的返回吗?-有没有一种方法来拦截postUpdate函数的成功或失败?
发布于 2016-09-12 19:52:05
CloudRail Android SDK是同步工作的,所以没有回调。这也是为什么应该使用某种形式的线程,因为您已经正确地这样做了。您可以简单地将post成功后要执行的代码放在该方法调用的后面。可以使用try-catch捕获错误。例如:
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();发布于 2016-09-12 21:06:09
这是我的最终解决方案。
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();
}
}我还使用了一个Handler,因为我希望将一个吐司作为回调显示给用户。
https://stackoverflow.com/questions/39447489
复制相似问题