Android-facebook-integration
Android-Facebook統合
Androidでは、アプリケーションをfacebookに接続して、facebookのデータやあらゆる種類の更新を共有できます。 この章では、facebookをアプリケーションに統合する方法について説明します。
Facebookを統合し、アプリケーションから何かを共有する方法は2つあります。 これらの方法は以下のとおりです-
- Facebook SDK
- インテントシェア
Facebook SDKの統合
これは、facebookと接続する最初の方法です。 アプリケーションを登録してからApplication Idを受け取り、facebook SDKをダウンロードしてプロジェクトに追加する必要があります。 手順は次のとおりです。
アプリケーション署名の生成
鍵署名を生成する必要がありますが、生成する前にSSLがインストールされていることを確認してください。そうでない場合は、SSlをダウンロードする必要があります。 https://code.google.com/p/openssl-for-windows/downloads/detail?name=openssl-0.9.8k_WIN32.zip&can=2&q= [こちら]からダウンロードできます。
コマンドプロンプトを開き、java jreフォルダーにリダイレクトします。 そこに到達したら、このコマンドを正確に入力します。 ウィンドウタブを選択し、設定タブを選択し、左側からandroidの下にあるビルドオプションを選択することで、Eclipseで見つけることができるキーストアパスで、逆コンマのパスを置き換える必要があります。
keytool -exportcert -alias androiddebugkey -keystore "your path"
| openssl sha1 -binary | openssl base64
入力すると、パスワードの入力が求められます。 パスワードとしてandroidを指定し、指定されたキーをコピーします。 それは以下の画像に示されています-
アプリケーションを登録する
developers.facebook.com/appsで新しいfacebookアプリケーションを作成し、すべての情報を入力します。 以下に示されています-
ネイティブAndroidアプリセクションに移動し、プロジェクトとクラス名を入力して、手順1でコピーしたハッシュを貼り付けます。 以下に示されています-
すべてが正常に機能する場合、シークレットを含むアプリケーションIDを受け取ります。 アプリケーションIDをコピーして、どこかに保存するだけです。 それは以下の画像に示されています-
SDKをダウンロードして統合する
facebook sdk https://github.com/facebook/facebook-android-sdk [こちら]をダウンロードします。 これをEclipseにインポートします。 インポートしたら、Facebookプロジェクトを右クリックしてプロパティをクリックします。Androidをクリックし、追加ボタンをクリックして、プロジェクトとしてfacebook SDKを選択します。[OK]をクリックします。
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() + "!");
}
}
});
}
}
}
インテントシェア
インテント共有は、アプリケーション間でデータを共有するために使用されます。 この戦略では、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 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を使用してFacebookでデータを共有する例を示す例です。 Facebookでテキストを共有できる基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
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.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 はfinddevguides.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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。
オプションとしてモバイルデバイスを選択し、デフォルト画面を表示するモバイルデバイスを確認します-
ボタンをタップするだけで、共有プロバイダーのリストが表示されます。
そのリストからfacebookを選択して、メッセージを書いてください。 それは以下の画像に示されています-