我创建了一个活动,使用此实现(请参见已接受的答案)在用户的Facebook主页上发布状态更新。
如果模拟器/手机没有安装Facebook应用程序,则可以正常使用。
如果模拟器/手机安装了Facebook应用程序,则Facebook应用程序将加载登录屏幕,但尝试登录后,Facebook应用程序只是消失并将我带回到我的应用程序。
有没有人在安装Facebook应用程序时遇到过这种情况?
我的代码:
如果模拟器/手机没有安装Facebook应用程序,则可以正常使用。
如果模拟器/手机安装了Facebook应用程序,则Facebook应用程序将加载登录屏幕,但尝试登录后,Facebook应用程序只是消失并将我带回到我的应用程序。
有没有人在安装Facebook应用程序时遇到过这种情况?
我的代码:
public class AchievementActivity extends Activity implements DialogListener, OnClickListener{
private Facebook facebook;
Button facebookPostButton;
String defaultFacebookPost;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.achievements);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_layout);
View achievementDivider = (View)findViewById(R.id.achievementDivider);
int[] colors = {0, 0xff00ffff, 0};
achievementDivider.setBackgroundDrawable(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
//get the title of the achievement from the intent that started this activity from the activity StatisticsActivity
String achievementTitleString = getIntent().getStringExtra("title");
String achievementTextToDisplay = getAchievementTextToDisplay(achievementTitleString);
defaultFacebookPost = getDefaultPost(achievementTitleString);
//ImageView achievementActivityAchievementBadgeImageView = (ImageView)findViewById(R.id.achievementActivityAchievementBadgeImageView);
TextView achievementActivityBadgeTitleTextView = (TextView)findViewById(R.id.achievementActivityBadgeTitleTextView);
achievementActivityBadgeTitleTextView.setText(achievementTitleString);
TextView achievementActivityAchievementText = (TextView)findViewById(R.id.achievementActivityAchievementText);
achievementActivityAchievementText.setText(achievementTextToDisplay);
facebookPostButton = (Button)findViewById(R.id.facebookPostButton);
facebookPostButton.setOnClickListener(this);
}
@Override
public void onComplete(Bundle values) {
if (values.isEmpty())
{
Toast.makeText(getApplicationContext(), "Empty", Toast.LENGTH_SHORT);
return;
}
if (!values.containsKey("post_id"))
{
try
{
Bundle parameters = new Bundle();
parameters.putString("message", defaultFacebookPost);// the message to post to the wall
facebook.dialog(AchievementActivity.this, "stream.publish", parameters, this);// "stream.publish" is an API call
}
catch (Exception e)
{
// TODO: handle exception
System.out.println(e.getMessage());
}
}
try
{
facebook.logout(getApplicationContext());
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFacebookError(FacebookError error)
{
Toast.makeText(AchievementActivity.this, "onFacebookError", Toast.LENGTH_LONG);
}
@Override
public void onError(DialogError e)
{
Toast.makeText(AchievementActivity.this, "onError", Toast.LENGTH_LONG);
}
@Override
public void onCancel()
{
Toast.makeText(AchievementActivity.this, "onCancel", Toast.LENGTH_LONG);
}
@Override
public void onClick(View v)
{
if (v == facebookPostButton)
{
facebook = new Facebook("my_facebook_api");
// replace APP_API_ID with your own
facebook.authorize(this, new String[] {"publish_stream", "read_stream", "offline_access"}, this);
}
}
private String getDefaultPost(String defaultTitleString)
{
//do some stuff here to get a string to post to wall
return defaultPost;
}
private String getAchievementTextToDisplay(String achievementTitleString) {
String achievementTextToDisplay = "DEFAULT";
//do some stuff here to get text to display in the activity
//this has nothing to do with the facebook post...
return achievementTextToDisplay;
}
}
Logcat告诉我:
05-11 13:03:34.076: INFO/ActivityManager(98): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) }
05-11 13:03:34.246: INFO/ActivityManager(98): Displayed activity com.facebook.katana/.ProxyAuth: 158 ms (total 158 ms)
05-11 13:03:35.166: DEBUG/dalvikvm(12390): GC_FOR_MALLOC freed 6729 objects / 418424 bytes in 44ms
05-11 13:03:35.166: DEBUG/webviewglue(12390): nativeDestroy view: 0x527e20
05-11 13:03:35.166: DEBUG/NativeCrypto(12390): Freeing OpenSSL session
编辑: 在过去一年中,我不得不在多台机器上安装Android开发平台,并且总是在创建新的开发环境后遇到Facebook的问题。我发现这个简单的答案可能是导致Facebook实现在一个设备上工作而在另一个设备上不工作的原因...
秘密的Facebook API密钥(你在developer.facebook.com上列出的那个)将因为每个版本的应用程序使用不同的证书打包而不同。例如,假设你有两台开发机器。由于这两台机器将使用不同的证书构建你的应用程序,你必须确保为每台机器生成一个Facebook API秘密密钥,并在developer.facebook.com上列出它们。