【问题标题】:Adding a Google +1 button in Android App在 Android 应用中添加 Google +1 按钮
【发布时间】:2012-01-12 22:51:59
【问题描述】:

我只是想知道是否可以在我的 Android 应用程序中添加一个 Google +1 按钮。 我在 Android Market 上看到了 +1,所以我认为会有一些方法可以做到这一点。

【问题讨论】:

  • 大家好,我已经为 +1 按钮生成了客户端 ID,但不知道在代码中的何处使用。有什么想法吗?

标签: android google-plus google-plus-one


【解决方案1】:

借助适用于 Android 的 Google+ 平台,您现在可以在自己的 Android 应用中集成原生 +1 按钮。

1) 您首先需要在 Activity 中 initialize PlusClient 对象。

2) 在布局中包含 PlusOneButton:

    <com.google.android.gms.plus.PlusOneButton
        xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
        android:id="@+id/plus_one_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        plus:size="standard"
        plus:annotation="inline" />

3) 将 PlusOneButton 分配给 Activity.onCreate 处理程序中的成员变量。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPlusClient = new PlusClient(this, this, this);
    mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
}

4) 每次 Activity 在 Activity.onResume 处理程序中获得焦点时刷新 PlusOneButton 的状态。

protected void onResume() {
    super.onResume();
    // Refresh the state of the +1 button each time the activity receives focus.
    mPlusOneButton.initialize(mPlusClient, URL);
}

更多信息请见https://developers.google.com/+/mobile/android/#recommend_content_with_the_1_button

【讨论】:

  • 您应该根据上次更新删除 Scopes.PLUS_PROFILE
  • PlusClient 构造函数不再受支持,您应该使用 PlusClient.Builder$build
  • 什么是 URL 变量?
  • 在我的例子中,我把链接放到了 Play 商店:play.google.com/store/apps/details?id=com.package.app
  • 这个答案已经过时了。 mPlusClient 不再需要,无论如何 mPlusOneButton.initialize() 不会接受它。看看here 了解新方法
【解决方案2】:

接受的答案已过时....

XML:

<com.google.android.gms.plus.PlusOneButton
  xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
  android:id="@+id/plus_one_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  plus:size="standard"
  plus:annotation="inline" />

活动:

// The request code must be 0 or greater.

    private static final int PLUS_ONE_REQUEST_CODE = 0;

protected void onResume() {
    super.onResume();
    // Refresh the state of the +1 button each time the activity receives focus.
    mPlusOneButton.initialize(URL, PLUS_ONE_REQUEST_CODE);
}

甚至在此之前,这个链接:

https://developers.google.com/+/mobile/android/getting-started

【讨论】:

  • PLUS_ONE_REQUEST_CODE 有什么用途?
【解决方案3】:

要添加 google plus one,首先您需要在开发者控制台中启用 API,然后使用包名称注册您的应用,然后将其包含在您的应用中。

这里是完整的例子和详细的解释。

http://www.feelzdroid.com/2014/09/google-plusone-1-button-in-android-application-integration-guide.html

【讨论】:

  • 感谢您的链接,它从基础解释...它节省了我的时间
  • 现在是否需要此步骤来实现 +1 按钮?
【解决方案4】:

使用新的 android studio(我正在使用的是 2.2.2),您可以更轻松地做到这一点。 内置功能可以使用 +1 按钮创建片段。您可以在活动中或任何您想要的地方使用 PlusOneButton 的布局或初始化代码。 查看下图:

编辑:不要忘记在 Google api 控制台中配置您的应用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多