• 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
广告

© . All rights reserved.