- Android 基础
- Android - 主页
- Android - 概述
- Android - 环境搭建
- Android - 架构
- Android - 应用组件
- Android - Hello World 示例
- Android - 资源
- Android - 活动 (Activities)
- Android - 服务 (Services)
- Android - 广播接收器 (Broadcast Receivers)
- Android - 内容提供器 (Content Providers)
- Android - 碎片 (Fragments)
- Android - 意图/过滤器 (Intents/Filters)
- Android - 用户界面
- Android - UI 布局
- Android - UI 控件
- Android - 事件处理
- Android - 样式和主题
- Android - 自定义组件
- Android 高级概念
- Android - 拖放
- Android - 通知
- 基于位置的服务
- Android - 发送邮件
- Android - 发送短信
- Android - 打电话
- 发布 Android 应用
- Android 实用示例
- Android - 警报对话框
- Android - 动画
- Android - 音频捕捉
- Android - 音频管理器
- Android - 自动完成
- Android - 最佳实践
- Android - 蓝牙
- Android - 相机
- Android - 剪贴板
- Android - 自定义字体
- Android - 数据备份
- Android - 开发者工具
- Android - 模拟器
- Android - Facebook 集成
- Android - 手势
- Android - Google 地图
- Android - 图片特效
- Android - ImageSwitcher
- Android - 内部存储
- Android - JetPlayer
- Android - JSON 解析器
- Android - LinkedIn 集成
- Android - 加载微调器
- Android - 本地化
- Android - 登录界面
- Android - MediaPlayer
- Android - 多点触控
- Android - 导航
- Android - 网络连接
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 进度圆圈
- Android - 进度条
- Android - 推送通知
- Android - RenderScript
- Android - RSS 阅读器
- Android - 屏幕录制
- Android - SDK 管理器
- Android - 传感器
- Android - 会话管理
- Android - 共享首选项
- Android - SIP 协议
- Android - 拼写检查
- Android - SQLite 数据库
- Android - 支持库
- Android - 测试
- Android - 文本转语音
- Android - TextureView
- Android - Twitter 集成
- Android - UI 设计
- Android - UI 模式
- Android - UI 测试
- Android - WebView 布局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 实用资源
- Android - 常见问题解答
- Android - 实用资源
- Android - 讨论
Android - LinkedIn 集成
Android 允许您的应用程序连接到 LinkedIn 并共享数据或任何类型的更新到 LinkedIn。本章将介绍如何将 LinkedIn 集成到您的应用程序中。
您可以通过两种方式集成 LinkedIn 并从您的应用程序中共享内容。这些方法列在下面。
LinkedIn SDK (Scribe)
Intent 共享
集成 LinkedIn SDK
这是连接 LinkedIn 的第一种方法。您需要注册您的应用程序,然后接收一些应用程序 ID,然后您需要下载 LinkedIn SDK 并将其添加到您的项目中。步骤如下所示。
注册您的应用程序
在 https://www.linkedin.com/secure/developer 创建一个新的 LinkedIn 应用程序。点击添加新应用程序。如下所示:
现在填写您的应用程序名称、描述和您的网站 URL。如下所示:
如果一切正常,您将收到一个带有密钥的 API 密钥。只需复制 API 密钥并将其保存在某个地方。如下图所示:
下载 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 运行该应用程序,请打开您的项目中的一个活动文件,然后点击工具栏中的运行
图标。在启动应用程序之前,Android Studio 将显示以下窗口以选择您要运行 Android 应用程序的位置。
选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕:
现在只需点击图像徽标,您将看到一个共享提供程序列表。
现在只需从该列表中选择 LinkedIn,然后编写任何消息。如下图所示:
现在它显示正在更新信息
