Android - Twitter 集成
Android 允许您的应用程序连接到 twitter 并在 twitter 上共享数据或任何类型的更新。 本章是关于将 twitter 集成到您的应用程序中。
您可以通过两种方式集成 twitter 并从您的应用程序中共享某些内容。 下面列出了这些方法 −
- Twitter SDK (Twitter4J)
- 意图共享
集成 Twitter SDK
这是连接 Twitter 的第一种方式。 您必须注册您的应用程序,然后接收一些应用程序 ID,然后您必须下载 twitter SDK 并将其添加到您的项目中。 步骤如下 −
注册您的应用程序
在 dev.twitter.com/apps/new 处创建一个新的 twitter 应用程序并填写所有信息。 如下图所示 −
现在在设置选项卡下,更改读取、写入和访问消息的访问权限并保存设置。 如下图所示 −
如果一切正常,您将收到一个带有密码的消费者 ID。 只需复制应用程序 ID 并将其保存在某处。 如下图所示 −
下载 SDK 并集成
在这里 http://twitter4j.org/en/下载 twitter sdk。 将 twitter4J jar 复制到您的项目 libs 文件夹中。
在 Twitter 应用上发布推文
一切都完成后,您可以运行 twitter 4J 示例,可以在这里 http://twitter4j.org/en/code-examples.html 找到该示例。
为了使用 twitter,你需要实例化一个 twitter 类的对象。可以通过调用静态方法 getsingleton() 来完成。 它的语法如下。
// The factory instance is re-usable and thread safe. Twitter twitter = TwitterFactory.getSingleton();
为了更新状态,您可以调用 updateStatus() 方法。 它的语法如下 −
Status status = twitter.updateStatus(latestStatus);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
意图共享
意图共享用于在应用程序之间共享数据。 在这个策略中,我们不会处理 SDK 的东西,而是让 twitter 应用程序来处理它。 我们将简单地调用 twitter 应用程序并将数据传递给共享。 这样,我们可以在 Twitter 上分享一些东西。
Android 提供了意图库来在活动和应用程序之间共享数据。 为了将其用作分享意图,我们必须将分享意图的类型指定为 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"));
除了这些方法之外,还有其他可用的方法可以进行意图处理。 它们在下面列出 −
| 序号 | 方法 & 描述 |
|---|---|
| 1 |
addCategory(String category) 此方法向意图添加一个新类别。 |
| 2 |
createChooser(Intent target, CharSequence title) 用于创建 ACTION_CHOOSER Intent 的便捷函数 |
| 3 |
getAction() 此方法检索要执行的一般操作,例如 ACTION_VIEW |
| 4 |
getCategories() 该方法返回意图中所有类别的集合和当前的缩放事件 |
| 5 |
putExtra(String name, int value) 此方法将扩展数据添加到意图。 |
| 6 |
toString() 此方法返回一个字符串,其中包含此对象的简明、人类可读的描述 |
示例
这是一个演示使用 IntentShare 在 twitter 上共享数据的示例。 它创建了一个基本应用程序,允许您在 twitter 上共享一些文本。
要试验这个例子,你可以在实际设备或模拟器上运行它。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android Studio 在 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.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class MainActivity extends ActionBarActivity {
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"));
}
});
}
}
以下是 res/layout/activity_main.xml 的修改内容。
<?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="Twitter 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 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的 Run
图标。在启动您的应用程序之前,Android Studio 将显示以下窗口以选择您要运行 Android 应用程序的选项。
选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 −
现在只需点击按钮,您就会看到共享提供商列表。
现在只需从该列表中选择 twitter,然后编写任何消息。 如下图所示 −
现在只需选择推文按钮,然后它将发布在您的推特页面上。 如下图所示 −

