2011-05-11 116 views
4

我已經構建了一個活動,使用this實現(請參閱接受的答案)在用戶的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上。

+0

您是否安裝了最新的FB應用更新?當我過去使用FB SDK時,我遇到了一些麻煩,導致FB應用程序未更新到最新版本(檢查市場應用程序,我的應用程序,確保FB應用程序沒有可用的更新)。 – 2011-05-12 06:48:17

+0

@Mathias Lin - 我使用的是FaceDook SKD附帶的Facebook版本。當你從gitHub獲取應用程序時,他們給你一個應用程序的編譯副本。 – dell116 2011-05-13 15:57:05

回答

1

這是因爲當您登錄Facebook帳戶時,您的登錄會話在設備中創建。完成任務後,您必須從Facebook註銷。

+0

感謝您的回覆,但我仍然無法獲得Facebook應用程序以保持活力。它只是消失。我編輯了我的問題以包含我的活動的代碼。 – dell116 2011-05-11 15:57:36

+0

請提供更詳細的答案! – tobias 2012-02-19 10:14:43

0
private static final String[] PERMISSIONS = 
     new String[] {"publish_stream", "read_stream", "offline_access"}; 




@Override 
public void onClick(View v) 
{ 
    if (v == facebookPostButton) 
    { 
     mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
            mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener()); 
         mFacebook.authorize(FacebookImplement.this, PERMISSIONS, new AuthorizeListener()); 
    } 
} 



public class AuthorizeListener extends BaseDialogListener { 

     public void onComplete(Bundle values) { 

      Bundle params = new Bundle(); 
      params.putString("message"," Message"); 
     params.putString("description", "Wall Posting Description");   
     mAsyncRunner.request("me/feed", params, "POST", 
       new UploadListener());   

     } 

}

public class UploadListener extends BaseRequestListener { 

     public void onComplete(final String response) { 
       mAsyncRunner.request(mFacebook.logout(getApplicationContext()), new LogoutListener()); 
} 

}

public class LogoutListener extends BaseRequestListener { 


     public void onComplete(final String response) {   



     } 
    } 

可能是這個代碼可以幫助你。如果有任何問題,請問沒有任何問題。

+0

非常感謝您提供您的代碼,但我仍然得到相同的結果。 Facebook應用程序打開,但立即關閉,並將我帶回我的應用程序的主要活動。此外,我建立了一個新項目,並在我的新項目中使用了來自www.facebook/developers的facebook代碼,並且facebook應用程序再次打開並立即關閉。這隻發生在設備已安裝facebook時,使用webview時一切正常。我在這裏完全失敗。 – dell116 2011-05-13 17:46:40

+0

你把我放在正確的軌道上,你的原始答案不正確登錄和退出會話是正確的。在看到上面的代碼並在github上查看示例後,我終於能夠正常工作。 – dell116 2011-05-13 20:35:44

+0

我有類似的問題。我不完全遵循你對正確登錄和退出會話的評論......你可以擴展一下嗎? – 2011-07-18 06:27:58

3

我發現了一個解決方法,但它並不是迄今爲止最好的。

facebook.authorize(activity, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, 
       new LoginDialogListener()); 

如果官方Facebook應用安裝在設備上,這將強制您的應用不使用SSO。但是,必須有更好的解決方案,因爲有應用程序在那裏使用官方Facebook應用程序的Sso。

相關問題