Android-twitter-integration
Android-Twitter統合
Androidを使用すると、アプリケーションはtwitterに接続して、twitterでデータやあらゆる種類の更新を共有できます。 この章では、twitterをアプリケーションに統合する方法について説明します。
twitterを統合し、アプリケーションから何かを共有するには、2つの方法があります。 これらの方法は以下のとおりです-
- Twitter SDK(Twitter4J)
- インテントシェア
Twitter SDKの統合
これは、Twitterと接続する最初の方法です。 アプリケーションを登録してからアプリケーションIDを受け取り、twitter SDKをダウンロードしてプロジェクトに追加する必要があります。 手順は以下のとおりです-
アプリケーションを登録する
dev.twitter.com/apps/newで新しいtwitterアプリケーションを作成し、すべての情報を入力します。 以下に示されています-
[設定]タブで、メッセージの読み取り、書き込み、アクセスへのアクセスを変更し、設定を保存します。 以下に示されています-
すべてが正常に機能する場合、シークレットを含むコンシューマーIDを受け取ります。 アプリケーションIDをコピーして、どこかに保存するだけです。 それは以下の画像に示されています-
SDKをダウンロードして統合する
twitter sdk こちらをダウンロードします。 twitter4J jarをプロジェクトのlibsフォルダーにコピーします。
Twitterアプリケーションにツイートを投稿する
すべてが完了したら、http://twitter4j.org/en/code-examplesl [こちら]にあるtwitter 4Jサンプルを実行できます。
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 finddevguides");
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() このメソッドは、インテント内のすべてのカテゴリのセットと現在のスケーリングイベントを返します |
5 |
putExtra(String name, int value) このメソッドは、拡張データをインテントに追加します。 |
6 |
toString() このメソッドは、このオブジェクトの人間が読める簡潔な説明を含む文字列を返します |
例
以下に、IntentShareを使用してTwitterでデータを共有する例を示します。 Twitterでテキストを共有できる基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Run the application and choose a running android device and install the application on it and verify the results |
変更された 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"));
}
});
}
}
以下は、xml 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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
オプションとしてモバイルデバイスを選択し、デフォルト画面を表示するモバイルデバイスを確認します-
ボタンをタップするだけで、共有プロバイダーのリストが表示されます。
そのリストからtwitterを選択して、メッセージを書き込んでください。 それは以下の画像に示されています-
ツイートボタンを選択すると、Twitterページに投稿されます。 以下に示されています-