我创建了一个用于连接Facebook的FbCnnect.java。我使用共享首选项来存储所有相关值。现在我创建了另一个Main.java,在这个页面中,每当我点击按钮它就必须启动一个意图UpdateStatus.java。在这个java类中,我想将我的状态更新到Facebook,没有登录屏幕,它必须自动完成。对于来自anther java类的getIntent()方法得到的值。 但它不起作用我认为......这是我更新状态的代码。有人请帮我这个代码。这样我就可以自动更新我的Facebook状态。 谢谢。
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Toast;
public class Main extends Activity {
private Facebook facebook;
private String messageToPost = ""+getIntent().getExtras().getString("message");
private String APP_ID = "";
private String[] PERMISSIONS = ""+getIntent().getExtras().getString("pubstream");
private String TOKEN = ""+getIntent().getExtras().getString("access_token");
private String EXPIRES =""+getIntent().getExtras().getString("expires_in");
private String KEY = ""+getIntent().getExtras().getString(""facebook-credentials");
facebook = new Facebook(APP_ID);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
restoreCredentials(facebook);
if (facebook.isSessionValid()) {
postToWall(messageToPost);
}
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") || response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHOW).show();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}