8

我正在制作应用程序,我想将它与 G+ 集成,以使用户使用 Google 帐户登录。要创建应用程序,我使用Android Studio

我在 Stack Overflow 上阅读了许多问题和答案,但我无法修复我的应用程序!

以下是我所做的步骤:
1. 将google-play-services.jar添加到我的项目的 libs 文件夹
2. 在 Android-Studio 项目资源管理器中的 jar 文件上单击“添加为库”
3. 从示例中的 java 文件中添加脚本web
4. 将google-play-services.jar库添加到Build.Gradle文件
5. 启动应用程序使其崩溃

问题是:如何在使用 Android Studio 制作的应用程序中添加 Google+ ingeration,我做错了什么吗?谢谢你的帮助

这是我的源代码:

Java 类:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.plus.PlusClient;

public class StartActivity extends Activity implements View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener {

    private static final String TAG = "ExampleActivity";
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_screen);

        mPlusClient = new PlusClient.Builder(this, this, this)
                .setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
                .build();

        mConnectionProgressDialog = new ProgressDialog(this);
        mConnectionProgressDialog.setMessage("Signing in...");

        initializeSplashElements();

    }


    ///     ***     ///         PLUS INTEGRATION        ///     ***     ////

    @Override
    protected void onStart() {
        super.onStart();
        mPlusClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlusClient.disconnect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (IntentSender.SendIntentException e) {
                mPlusClient.connect();
            }
        }
        // Save the result and resolve the connection failure upon a user click.
        mConnectionResult = result;
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
            mConnectionResult = null;
            mPlusClient.connect();
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        String accountName = mPlusClient.getAccountName();
        Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDisconnected() {
        Log.d(TAG, "disconnected");
    }

    ///     ***     ///         PLUS INTEGRATION        ///     ***     ///

    (...)

}

布局 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:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".StartActivity"
              android:clickable="false"
              android:background="@drawable/ziel_pomar_tlo">

    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:id="@+id/splashLogo"
            android:src="@drawable/catch_up_logo_flatten"
            android:contentDescription="@string/catchup_logo_describe"
            android:layout_marginTop="40dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:focusable="false"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/loading_3dot"
            android:id="@+id/loadingText"
            android:enabled="true"
            android:gravity="center"
            android:textSize="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:layout_marginBottom="35dp"/>

    <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView"
            android:layout_above="@+id/loadingText"
            android:layout_below="@+id/splashLogo"
            android:layout_margin="10dp"
            android:layout_alignParentEnd="false"
            android:layout_centerHorizontal="true">

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:clickable="false"
                >

            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top"
                    android:id="@+id/gps_row">

                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/loading_screen_no_gps"
                        android:id="@+id/gps_desc"
                        android:textSize="20dp"/>

                <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:weightSum="3"
                        android:layout_marginTop="10dp">

                    <Button
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/loading_screen_no_gps_settings"
                            android:id="@+id/gps_settings"
                            android:layout_weight="2"/>

                    <Button
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/loading_screen_no_gps_ok"
                            android:id="@+id/gps_fixed"
                            android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/web_row"
                    android:layout_gravity="bottom|top"
                    android:layout_marginTop="20dp">

                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/loading_screen_no_web"
                        android:id="@+id/web_desc"
                        android:textSize="20dp"/>

                <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:weightSum="3"
                        android:layout_marginTop="10dp">

                    <Button
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/loading_screen_no_web_settings"
                            android:id="@+id/web_wifi_settings"
                            android:layout_weight="1"/>

                    <Button
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/loading_screen_no_web_settings2"
                            android:id="@+id/web_3g_settings"
                            android:layout_weight="1"/>

                    <Button
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/loading_screen_no_web_ok"
                            android:id="@+id/web_fixed"
                            android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

构建.Gradle:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar', 'libs/google-play-services.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 17
        compileSdkVersion 17
    }
}
4

2 回答 2

9

您可以使用 Google 存储库,而无需在代码中导入任何库 (.jar)

更新:如果您正在寻找 Google(或第三方)库,您可以查看:http://gradleplease.appspot.com/(由 Google 开发人员 Chris Broadfoot 提供)

dependencies {
    compile 'com.google.android.gms:play-services:4.4.52'
    compile 'com.android.support:support-v4:19.1.0'
    // compile files('libs/android-support-v4.jar') // OLD: can remove

    // Other useful library
    // compile 'com.android.support:appcompat-v7:19.1.0'
    // compile 'com.android.support:gridlayout-v7:19.1.0'
    // compile 'com.android.support:mediarouter-v7:19.1.0'
    // compile 'com.android.support:support-v13:19.1.0'

    // Note: these libraries require the "Google Repository" and "Android Repository"
    //       to be installed via the SDK manager.
}

参考: https: //plus.google.com/+AndroidDevelopers/posts/4Yhpn6p9icf

于 2013-07-12T12:40:25.647 回答
0

android studio 中的 Google plus 集成非常简单,请按照以下步骤操作。

1.将播放服务依赖添加到build.gradle。

2.将json配置文件添加到你的应用目录,你可以从谷歌开发者控制台获取它

3.Build GoogleSignInOption 和 GoogleApiClient 并在按钮点击时开始谷歌登录..

有关更多详细信息,您可以通过本教程 Google plus Integration in android studio

于 2016-02-15T18:49:27.567 回答