2013-02-20 32 views
11

Tôi hiện đang thực hiện một dự án nhỏ muốn có nút chia sẻ (hộp thoại), khi người dùng nhấp vào nó, nó sẽ tự động đăng nhập vào tài khoản fb của anh ta/cô ấy và chia sẻ nội dung họ muốn.Facebook SDK Android 3.0, cách chia sẻ nội dung mà không có LoginButton

Sau hướng dẫn từ fb dev, ứng dụng của tôi có thể chia sẻ nội dung lên tường, nhưng cần đăng nhập bằng nút fblogin trước khi chia sẻ.

Tôi đã đọc một bài đăng từ stackoverflow: Android - Facebook SDK 3 - How to login programmatically without LoginButton

CẬP NHẬT: Tôi có thực hiện feedDialog với onActivityResult trong dự án của tôi, tôi thấy rằng tôi có thể đăng nhập và chia sẻ với một nút bấm. HOWEVER, khi tôi xây dựng lại ứng dụng/khởi động lại điện thoại, tôi phải nhấn nút hai lần để chia sẻ lần đầu tiên, nhưng trở thành bình thường (nhấn một lần) sau.

P.S.I có thực hiện nó với ShareActionProvider

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getSupportMenuInflater().inflate(R.menu.content_main, menu); 
    /** Getting the actionprovider associated with the menu item whose id is share */ 
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider(); 

    /** Getting the target intent */ 
    Intent intent = getDefaultShareIntent(); 

    /** Setting a share intent */  
    if(intent!=null){ 
     mShareActionProvider.setShareIntent(intent); 
     mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){ 
      @Override 
      public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) { 
       if ("com.facebook.katana".equals(intent.getComponent().getPackageName())){ 
        if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) { 
         Session.openActiveSession(Content.this, true, null); 
       }else{ 
         publishFeedDialog(); 
        } 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 

    return super.onCreateOptionsMenu(menu); 
} 
@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
     if (Session.getActiveSession() != null || Session.getActiveSession().isOpened()) 
      publishFeedDialog(); 
    } 
private void publishFeedDialog() { 
     Bundle params = new Bundle(); 
     params.putString("name", "ab"); 
     params.putString("caption", "cd"); 
     params.putString("description", "def"); 
     params.putString("link", "https://developers.facebook.com/android"); 
     params.putString("picture", "abc.jpg"); 

     WebDialog feedDialog = (
      new WebDialog.FeedDialogBuilder(Content.this, 
       Session.getActiveSession(), 
       params)) 
      .setOnCompleteListener(new OnCompleteListener() { 

       @Override 
       public void onComplete(Bundle values, 
        FacebookException error) { 
        if (error == null) { 
         // When the story is posted, echo the success 
         // and the post Id. 
         final String postId = values.getString("post_id"); 
         if (postId != null) { 
          Toast.makeText(Content.this, 
           "Posted story, id: "+postId, 
           Toast.LENGTH_SHORT).show(); 
         } else { 
          // User clicked the Cancel button 
          Toast.makeText(Content.this.getApplicationContext(), 
           "Publish cancelled", 
           Toast.LENGTH_SHORT).show(); 
         } 
        } else if (error instanceof FacebookOperationCanceledException) { 
         // User clicked the "x" button 
         Toast.makeText(Content.this.getApplicationContext(), 
          "Publish cancelled", 
          Toast.LENGTH_SHORT).show(); 
        } else { 
         // Generic, ex: network error 
         Toast.makeText(Content.this.getApplicationContext(), 
          "Error posting story", 
          Toast.LENGTH_SHORT).show(); 
        } 
       } 

      }) 
      .build(); 
     feedDialog.show(); 
     } 
+0

Đi qua Ming Li câu trả lời và bình luận của ông (thứ hai). –

+0

tôi biết, nhưng tôi thực sự muốn giải thích thêm một chút hoặc mã becox tôi không hiểu ........ i am mới cho android dev –

+0

@CalvinChan bạn đã có thể để kéo này đi? Tôi đang tìm giải pháp tương tự mà tôi sẽ chia sẻ hình ảnh – Casper

Trả lời

11

Tôi nghĩ rằng vấn đề ở đây là khi bạn đang xây dựng trình đơn, bạn kiểm tra xem phiên hoạt động có là không, và nếu có, bạn gọi openActiveSession và chờ onActivityResult được gọi. Đây là tất cả chính xác, BAO GIỜ, nếu trước đó người dùng đã cho phép ứng dụng của bạn, thì mã thông báo truy cập, v.v ... đều được lưu trong bộ nhớ cache mã thông báo và gọi openActiveSession sẽ thực sự mở phiên ngay lập tức (không gọi onActivityResult).

Cách chính xác thực là chuyển qua một StatusCallback tới cuộc gọi đang mở của bạn (thay vì null) và trong cuộc gọi đó, hãy kiểm tra xem phiên có đang mở hay không và gọi phương thức publishFeedDialog của bạn.

0

LoginButton trong facebook-sdk-3.0 chỉ là một công cụ để có được một phiên làm việc từ FB.

Nếu bạn đã nhận được một phiên từ FB, bạn cần phải kiểm tra quyền xuất bản.

+0

tôi biết, tôi đã thực hiện nó với một loginButton. Nhưng những gì tôi muốn là làm cho nó KHÔNG CÓ loginButton –

15

Cảm ơn Ming Li rất nhiều cho các giải pháp

cuối cùng tôi nhận được câu trả lời !!!! Dưới đây là mã, hy vọng nó có thể giúp các nhà phát triển khác

private Session.StatusCallback callback = new Session.StatusCallback() { 
      @Override 
      public void call(Session session, SessionState state, 
      Exception exception) { 
      onSessionStateChange(session, state, exception); 
      } 
    }; 
    private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
     if (state.isOpened()) { 
      publishFeedDialog(); 
     } 
    } 
    //........................ 
        if (Session.getActiveSession() == null || Session.getActiveSession().isClosed()) { 
          Session.openActiveSession(Content.this, true, callback); 
        }else{ 
          publishFeedDialog(); 
         } 
    //....................... 
    @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
     } 
    private void publishFeedDialog() { 
      //........................ 
     } 
+0

Cảm ơn bạn rất nhiều, hoạt động như một sự quyến rũ – Casper

4

Đây được bao phủ ngay trong Getting Started hướng dẫn:

package com.firstandroidapp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.widget.TextView; 
import com.facebook.*; 
import com.facebook.model.*; 

public class MainActivity extends Activity { 

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

// start Facebook Login 
Session.openActiveSession(this, true, new Session.StatusCallback() { 

    // callback when session changes state 
    @Override 
    public void call(Session session, SessionState state, Exception exception) { 
    if (session.isOpened()) { 

     // make request to the /me API 
     Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 

     // callback after Graph API response with user object 
     @Override 
     public void onCompleted(GraphUser user, Response response) { 
      if (user != null) { 
      TextView welcome = (TextView) findViewById(R.id.welcome); 
      welcome.setText("Hello " + user.getName() + "!"); 
      } 
     } 
     }); 
    } 


    } 
    }); 
    } 

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

} 
Các vấn đề liên quan