• Android Video Tutorials

Android - Facebook 整合



Android 允許您的應用程式連線到 Facebook 並共享資料或任何型別的更新到 Facebook。本章介紹如何將 Facebook 整合到您的應用程式中。

您可以透過兩種方式整合 Facebook 並從您的應用程式中共享內容。這些方式列在下面:

  • Facebook SDK
  • Intent 共享

整合 Facebook SDK

這是連線 Facebook 的第一種方式。您需要註冊您的應用程式,然後接收一些應用程式 ID,然後您需要下載 Facebook SDK 並將其新增到您的專案中。步驟如下所示

生成應用程式簽名

您需要生成一個金鑰簽名,但在生成它之前,請確保您已安裝 SSL,否則您需要下載 SSL。它可以從 這裡 下載。

現在開啟命令提示符並重定向到您的 Java JRE 資料夾。到達那裡後,準確地鍵入此命令。您需要將引號中的路徑替換為您金鑰庫的路徑,您可以在 Eclipse 中找到它,方法是選擇“視窗”選項卡,然後選擇“首選項”選項卡,然後在左側選擇 Android 下的“構建”選項。

keytool -exportcert -alias androiddebugkey -keystore "your path" 
   | openssl sha1 -binary | openssl base64

輸入後,系統將提示您輸入密碼。輸入 android 作為密碼,然後複製給您的金鑰。它顯示在下面的影像中:

Android Facebook Tutorial

註冊您的應用程式

現在在 developers.facebook.com/apps 建立一個新的 Facebook 應用程式,並填寫所有資訊。它顯示在下面:

Android Facebook Tutorial

現在轉到原生 Android 應用部分,填寫您的專案和類名,並將您在步驟 1 中複製的雜湊貼上到其中。它顯示在下面:

Android Facebook Tutorial

如果一切正常,您將收到一個包含金鑰的應用程式 ID。只需複製應用程式 ID 並將其儲存到某個位置。它顯示在下面的影像中:

Android Facebook Tutorial

下載 SDK 並整合它

這裡 下載 Facebook SDK。將其匯入到 Eclipse 中。匯入後,右鍵單擊您的 Facebook 專案並單擊“屬性”。單擊“Android”,單擊“新增”按鈕,然後選擇 Facebook SDK 作為專案。單擊“確定”。

建立 Facebook 登入應用程式

一旦所有操作都完成,您可以執行 SDK 附帶的示例或建立您自己的應用程式。為了登入,您需要呼叫 openActiveSession 方法並實現其回撥。其語法如下所示:

// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
   
   // callback when session changes state
   public void call(Session session, SessionState state, Exception exception) {
      if (session.isOpened()) {
         // make request to;2 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() + "!");
               }
            }
         });
      }
   }
}

Intent 共享

Intent 共享用於在應用程式之間共享資料。在這種策略中,我們不會處理 SDK 方面的內容,而是讓 Facebook 應用程式處理它。我們將簡單地呼叫 Facebook 應用程式並將資料傳遞給它以進行共享。透過這種方式,我們可以共享一些內容到 Facebook。

Android 提供 Intent 庫來在 Activity 和應用程式之間共享資料。為了將其用作共享 Intent,我們必須將共享 Intent 的型別指定為 ACTION_SEND。其語法如下所示:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);

接下來,您需要定義要傳遞的資料型別,然後傳遞資料。其語法如下所示:

shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your thoughts"));

除了這些方法之外,還有其他可用的方法允許 Intent 處理。它們列在下面:

序號 方法及描述
1

addCategory(String category)

此方法向 Intent 新增一個新的類別。

2

createChooser(Intent target, CharSequence title)

建立 ACTION_CHOOSER Intent 的便捷函式

3

getAction()

此方法檢索要執行的常規操作,例如 ACTION_VIEW

4

getCategories()

此方法返回 Intent 中所有類別的集合以及當前縮放事件

5

putExtra(String name, int value)

此方法向 Intent 新增擴充套件資料。

6

toString()

此方法返回一個字串,其中包含此物件的簡潔、易於理解的描述

示例

這是一個演示如何使用 IntentShare 在 Facebook 上共享資料的示例。它建立一個基本的應用程式,允許您在 Facebook 上共享一些文字。

要試驗此示例,您可以在實際裝置或模擬器上執行它。

步驟 描述
1 您將使用 Android Studio 在包 com.example.sairamkrishna.myapplication 下建立一個 Android 應用程式。
2 修改 src/MainActivity.java 檔案以新增必要的程式碼。
3 修改 res/layout/activity_main 以新增相應的 XML 元件。
4 執行應用程式並選擇一個正在執行的 Android 裝置,並在其上安裝應用程式並驗證結果。

以下是修改後的主 Activity 檔案 MainActivity.java 的內容。

package com.example.sairamkrishna.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import android.widget.Button;
import android.widget.ImageView;

import java.io.FileNotFoundException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
   private ImageView img;

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      img=(ImageView)findViewById(R.id.imageView);
      Button b1=(Button)findViewById(R.id.button);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            Uri screenshotUri = Uri.parse("android.
            resource://comexample.sairamkrishna.myapplication/*");
            
            try {
               InputStream stream = getContentResolver().openInputStream(screenshotUri);
            } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }

            sharingIntent.setType("image/jpeg");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
            startActivity(Intent.createChooser(sharingIntent, "Share image using"));
         }
      });
   }
}

以下是修改後的 xml res/layout/activity_main.xml 的內容。

在下面的程式碼中,abc 表示 tutorialspoint.com 的徽標
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:text="Facebook share " />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true"
      android:src="@drawable/abc"/>
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Share"
      android:id="@+id/button"
      android:layout_marginTop="61dp"
      android:layout_below="@+id/imageView"
      android:layout_centerHorizontal="true" />
      
</RelativeLayout>

以下是 AndroidManifest.xml 檔案的內容。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
   </application>
</manifest>

讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟您的專案之一的 Activity 檔案,然後單擊工具欄中的“執行”Eclipse Run Icon 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇您要在其中執行 Android 應用程式的選項。

Android facebook Tutorial

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕:

Android facebook Tutorial

現在只需點選按鈕,您將看到一個共享提供程式列表。

Android facebook Tutorial

現在只需從該列表中選擇 Facebook,然後寫下任何訊息。它顯示在下面的影像中:

Android facebook Tutorial
廣告