2012-09-29 61 views
0

嗨,大家好我是集成facebook登錄到我的應用程序。我已經搜索了很多,我可以登錄我已經使用這個鏈接:https://developers.facebook.com/docs/mobile/android/build/#register
我想要做的就是當我點擊一個按鈕,然後facebook對話框必須出現intend只是打開活動(HomeActivity)以下是我的碼。在Facebook上打開Facebook登錄對話單擊

public class HomeActivity extends Activity { 

    Facebook facebook = new Facebook("114987225319269"); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     Button more = (Button) findViewById(R.id.button1); 
     more.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       facebook.authorize(this, new DialogListener() { 
        @Override 
        public void onComplete(Bundle values) {} 

        @Override 
        public void onFacebookError(FacebookError error) {} 

        @Override 
        public void onError(DialogError e) {} 

        @Override 
        public void onCancel() {} 
       }); 
      } 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 

       facebook.authorizeCallback(requestCode, resultCode, data); 
      } 

     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_home, menu); 
     return true; 
    } 
} 
+0

我想的是,按鈕我添加在主屏幕上,當我點擊該按鈕,然後出現Facebook登錄對話框。但是這個代碼給出了錯誤。但是,如果沒有按鈕點擊偵聽器,我運行這段代碼,然後它沒有錯誤,運行良好。請幫助...瞭解我想告訴的是什麼? – Nadeem

+0

嘗試以下鏈接: 它爲我.. http://stackoverflow.com/a/10959330/1278196 – moDev

+0

沒有u得到任何錯誤?張貼您的logcat – MAC

回答

0

使用此代碼

Button more = (Button) findViewById(R.id.button1); 
more.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     if (! facebook.isSessionValid()) {   
      facebook.authorize(HomeActivity.this, PERMISSIONS, new LoginDialogListener()); 
     }); 
    } 

這是登錄對話框聽衆

private class LoginDialogListener implements DialogListener { 

    public void onComplete(Bundle values) { 
     saveCredentials(facebook); 
     getAlbumsData task = new getAlbumsData();   
     task.execute(); 
    } 

    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!"); 
    } 
} 
+0

非常感謝RajaReddy ...它真的爲我工作..再次感謝.. – Nadeem

+0

嗨拉賈你能告訴我在做什麼錯誤?你的代碼的邏輯是什麼?所以下次我可以避免這個錯誤。謝謝 – Nadeem

+0

我創建登錄對話框作爲一個單獨的方法,之後,我在點擊按鈕中調用該方法。而已。如果你不明白讓我知道你的疑問.. –

0

在您的清單

<activity 
android:theme="@android:style/Theme.Translucent" 
android:name=".HomeActivity " 
> 

這將改變你的活動主題爲Dialog主題。希望這有助於...

+0

它也工作..非常感謝Atif – Nadeem

0

這是所有你需要的東西:

public class BaseActivity extends Activity 
{ 

    public AsyncFacebookRunner mAsyncRunner; 
    public Facebook facebook; 
    public SharedPreferences mPrefs; 
    public static String appId = "123344"; 
    SharedPreferences.Editor editor; 


    private void initialize() 
    { 

     facebook = new Facebook(appId); 
     mAsyncRunner = new AsyncFacebookRunner(facebook); 

    } 

    public void verifyLogin() 
    { 

     mPrefs = getPreferences(MODE_PRIVATE); 
     String accessToken = mPrefs.getString("access_token", null); 
     facebook.setAccessToken(accessToken); 
     long expires = mPrefs.getLong("access_expires", 0); 

     if (accessToken != null) 
     { 
      facebook.setAccessToken(accessToken); 
     } 

     if (expires != 0) 
     { 
      facebook.setAccessExpires(expires); 
     } 

     if (!facebook.isSessionValid()) 
     { 
      facebook.authorize(this, new String[] { 
       "email", "publish_stream", "create_event" 
      }, new DialogListener() 
      { 

       @Override 
       public void onCancel() 
       { 
        // Function to handle cancel event 
       } 

       @Override 
       public void onComplete(Bundle values) 
       { 
        // Function to handle complete event 
        // Edit Preferences and update facebook acess_token 
        editor = mPrefs.edit(); 
        editor.putString("access_token", facebook.getAccessToken()); 
        editor.putLong("access_expires", facebook.getAccessExpires()); 
        editor.commit(); 
       } 

       @Override 
       public void onError(DialogError error) 
       { 
        // Function to handle error 

       } 

       @Override 
       public void onFacebookError(FacebookError fberror) 
       { 
        // Function to handle Facebook errors 

       } 

      }); 
     } 

    } 

} 
相關問題