• Android Video Tutorials

Android - LinkedIn 整合



Android 允許您的應用程式連線到 LinkedIn 並共享資料或任何型別的更新到 LinkedIn。本章將介紹如何將 LinkedIn 整合到您的應用程式中。

您可以透過兩種方式整合 LinkedIn 並從您的應用程式中共享內容。這些方法列在下面。

  • LinkedIn SDK (Scribe)

  • Intent 共享

整合 LinkedIn SDK

這是連線 LinkedIn 的第一種方法。您需要註冊您的應用程式,然後接收一些應用程式 ID,然後您需要下載 LinkedIn SDK 並將其新增到您的專案中。步驟如下所示。

註冊您的應用程式

https://www.linkedin.com/secure/developer 建立一個新的 LinkedIn 應用程式。點選新增新應用程式。如下所示:

Android Linkedin Tutorial

現在填寫您的應用程式名稱、描述和您的網站 URL。如下所示:

Android Linkedin Tutorial

如果一切正常,您將收到一個帶有金鑰的 API 金鑰。只需複製 API 金鑰並將其儲存在某個地方。如下圖所示:

Android Linkedin Tutorial

下載 SDK 並整合它

下載 LinkedIn sdk 這裡。將 scribe-1.3.0.jar jar 複製到您的專案 libs 資料夾中。

在 LinkedIn 應用程式上釋出更新

一旦一切完成,您可以執行 LinkedIn 示例,可以在 這裡 找到。

Intent 共享

Intent 共享用於在應用程式之間共享資料。在這種策略中,我們不會處理 SDK 相關的東西,而是讓 LinkedIn 應用程式處理它。我們將簡單地呼叫 LinkedIn 應用程式並將要共享的資料傳遞給它。這樣,我們就可以在 LinkedIn 上共享一些內容。

Android 提供了 Intent 庫來在活動和應用程式之間共享資料。為了將其用作共享 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 在 LinkedIn 上共享資料的示例。它建立一個基本的應用程式,允許您在 LinkedIn 上共享一些文字。

要試驗此示例,您可以在實際裝置或模擬器上執行它。

步驟 描述
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.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="Linkedin 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/logo"/>
      
   <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 執行該應用程式,請開啟您的專案中的一個活動檔案,然後點選工具欄中的執行 Eclipse Run Icon 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇您要執行 Android 應用程式的位置。

Android Linkedin Tutorial

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕:

Android Linkedin Tutorial

現在只需點選影像徽標,您將看到一個共享提供程式列表。

Android Linkedin Tutorial

現在只需從該列表中選擇 LinkedIn,然後編寫任何訊息。如下圖所示:

Android Linkedin Tutorial

現在它顯示正在更新資訊

Android Twitter Tutorial
廣告