Android允许您的应用程序连接到Facebook并在Facebook上共享数据或任何类型的更新.本章是关于将facebook集成到您的应用程序中.
您可以通过两种方式集成Facebook并从应用程序中共享内容.这些方式列在下面和下面;
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作为密码,然后复制给您的密钥.它显示在下方和下方的图像中;
现在在 developers.facebook.com/apps 上创建一个新的Facebook应用程序并填写所有信息.它显示在下面 :
现在转到原生Android应用程序部分并填写您的项目和类名称并粘贴您在步骤1中复制的哈希.它显示在下面 :
如果一切正常,您将收到带有秘密的应用程序ID.只需复制应用程序ID并将其保存在某处.它显示在下方和下方的图像中;
下载facebook sdk.将此导入eclipse.导入后,右键单击您的Facebook项目并单击属性.单击android,单击添加按钮并选择facebook sdk作为项目.单击确定.
一切都完成后,您可以运行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() + "!"); } } }); } } }
意图共享用于在应用程序之间共享数据.在这个策略中,我们不会处理SDK的东西,但让facebook应用程序处理它.我们将简单地调用facebook应用程序并传递数据进行共享.通过这种方式,我们可以在Facebook上分享一些内容.
Android提供了意图库,用于在活动和应用程序之间共享数据.为了将其用作共享意图,我们必须为 ACTION_SEND 指定共享意图的类型.它的语法在下面和下面给出;
Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND);
您需要的下一件事是定义要传递的数据类型,然后传递数据.它的语法在下面和下面给出;
shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from IT屋"); startActivity(Intent.createChooser(shareIntent, "Share your thoughts"));
除了这些方法之外,还有其他可用的方法可以进行意图处理.它们列在下面和下面;
Sr.No | Method & description |
---|---|
1 | addCategory(String category) 此方法为意图添加新类别. |
2 | createChooser(Intent target, CharSequence title) 创建ACTION_CHOOSER意图的便利功能 |
3 | getAction( ) 此方法检索要执行的常规操作,例如ACTION_VIEW |
4 | getCategories() 此方法返回intent和当前缩放事件中所有类别的集合 |
5 | putExtra(String name,int value ) 此方法广告d扩展数据到意图. |
6 | toString() 此方法返回包含简洁,人性化的字符串这个对象的可读描述 |
这是一个演示如何使用IntentShare在Facebook上共享数据的示例.它创建了一个基本应用程序,允许您在Facebook上共享一些文本.
要试验这个例子,您可以在实际设备或模拟器中运行它.
步骤 | 描述 |
---|---|
1 | 您将使用Android工作室在com.example.sairamkrishna.myapplication包下创建一个Android应用程序. |
2 | 修改src/MainActivity.java文件以添加必要的代码. |
3 | 修改res/layout/activity_main以添加相应的XML组件. |
4 | 运行应用程序并选择正在运行安装Android设备并在其上安装应用程序并验证结果. |
以下是修改后的主要活动文件 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 表示it1352的标识.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工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏中.在开始申请之前,Android studio将显示以下窗口,以选择您要运行Android应用程序的选项.
选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 :
现在只需点击按钮,您就会看到一个共享提供商列表.
现在只需从该列表中选择facebook然后再写任何消息.它显示在下面的图像中 :