2012-11-22 66 views
5

我使用intent和Action.SEND在WhatsApp,twitter,Facebook和GMail等社交網絡上共享我的自定義消息。除了Facebook之外,Gmail和其他應用程序都可以使用!我如何自定義我的代碼以在Facebook上分享內容?我使用Facebook SDK在Facebook上分享,沒有問題,但我想用意圖來做。在FaceBook上共享內容Android

這是我使用:

Intent sendIntent = new Intent(); 
sendIntent.setAction(Intent.ACTION_SEND); 
sendIntent.putExtra(Intent.EXTRA_TEXT, knowTitle+"Read the full article via MomsApp by EnfaMama A+ at http://meadjohnsonasia.com.my/mobileapp"); 

sendIntent.putExtra(Intent.EXTRA_SUBJECT, "I just read "+knowTitle); 
sendIntent.setType("*/*"); 
startActivity(Intent.createChooser(sendIntent, "Share Your Favorite Article")); 

回答

5

我所做的實際上是截取intenthandlers的選定目標,你可以通過使用你的actionprovider來實現。假設您創建了一個通過onclick開始意圖的項目。爲了做到這一點,你可以實例化一個actionprovider來做到這一點。這個actionprovider可以有一個setOnShareTargetSelectedListener來截取你想要處理的任何意圖(或根本不^ ^)。有關如何配置actionprovider的信息,請參閱以下代碼。

actionProvider.setShareIntent(createShareIntent()); 
    actionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){ 

     @Override 
     public boolean onShareTargetSelected(ShareActionProvider source, 
       Intent intent) { 
      if ("com.facebook.katana".equals(intent.getComponent().getPackageName()) && mfacebooksharer != null) { 
       mfacebooksharer.shareStatus(subject, text); 
        return true; 
       } 
       return false; 
     } 

    }); 

每當選擇Facebook的,我用我的mfacebooksharer來處理這個意圖,並按照Facebook的API。 當然,這個actionrpovider需要有一個意圖。 (就像你想用意圖一樣)。我使用下面的方法來創建意圖。

private Intent createShareIntent() { 
     intentsetter.setIntentleave(true); 
     Intent shareIntent = new Intent(Intent.ACTION_SEND); 
     shareIntent.setType("text/plain"); 
     shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
     shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);   
     return shareIntent; 
    } 
+0

這隻適用於Android 4.0 – Giorgi

1

在Android中使用的意圖,你只能共享一個鏈接沒有文字:

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_TEXT, "http://www.google.ca"); 
startActivity(Intent.createChooser(intent, "Share with")); 

它會工作。如果你想分享文本和鏈接,你必須使用Facebook SDK for Android:https://github.com/facebook/facebook-android-sdk

4

根據Facebook的平臺政策,您不能使用 Intent.EXTRA_TEXT預填充共享對話框。它通常被認爲是一個bug,但是根據一份Bug Report here以及here,Facebook明確提到這不是問題(它不是一個錯誤)。

你可以閱讀更多關於他們的具體Platform Policies平臺策略.2

從平臺策略IV.2報價:

你不能預先填充任何字段與下列 產品相關聯,除非用戶手動生成 之前的內容工作流程:流式故事(user_message參數爲 Facebook.streamPublish和FB.Connect.st reamPublish,和消息 參數爲stream.publish),照片(標題),視頻(描述), 註釋(標題和內容),鏈接(評論)和Jabber/XMPP。

這些字段旨在讓用戶表達自己。預填充 這些字段會侵蝕用戶語音的真實性。

的唯一方法,你可以分享故事從您的應用程序是通過整合Facebook的SDK,它按照你的帖子,你已經能夠成功。這是唯一可用的選項(不幸)。

+0

確定,但我希望用戶使用Facebook的SDK直接到我的自定義Facebook的分享,我怎麼能檢測用戶點擊Facebook和Gmail的未比如!那麼如果它的Facebook我可以輕鬆地做我想要的東西 –

+0

@MahdiGiveie:你想讓用戶從他們可以共享的應用程序列表中選擇你的應用程序? –

+0

不是我的應用程序!我想要用戶點擊Facebook上的應用程序列表中的用戶可以直接分享到我的自定義Facebook共享!我的問題是我怎麼能檢測到用戶點擊Facebook上的名單? –