从Android应用程序中共享Google Plus

德罗捷夫

我必须分享一些文本和来自android应用程序的链接,因为我编写了如下代码

        PlusShare.Builder shareBuilder = new PlusShare.Builder(this);
        shareBuilder.setType("text/plain");
        shareBuilder.setText("text to be shared");
        shareBuilder.setContentUrl(Uri.parse("link_to_share"));
        Intent shareIntent = shareBuilder.getIntent();
        startActivityForResult(shareIntent, SHARE_GOOGLE_PLUS_REQUEST_CODE);

此代码可以正常工作,我可以与Google Plus共享,并且可以通过onActivityResult()进行回拨。我的问题是,如果用户未安装google plus,则应用程序停止响应并退出。我该如何解决

安卓系统

尝试以下代码:

// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(ThirtyArticleDetail.this)
    .addConnectionCallbacks(ThirtyArticleDetail.this)
    .addOnConnectionFailedListener(ThirtyArticleDetail.this).addApi(Plus.API)
    .addScope(Plus.SCOPE_PLUS_LOGIN).build();

btnsharegplus.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                if (!mGoogleApiClient.isConnected()) {
                    // Set sharing so that the share is started in onConnected.
                    mSignInClicked= true;

                    if (!mGoogleApiClient.isConnecting()) {
                        mGoogleApiClient.connect();
                    }
                } else {
                    Intent shareIntent = new PlusShare.Builder(ThirtyArticleDetail.this)
                    .setType("text/plain")
                    .setText("Welcome to the Google+ platform.")
                    .setContentUrl(Uri.parse(articleurl))
                    //.setContentUrl(Uri.parse("https://developers.google.com/+/"))
                    .getIntent();

                    startActivityForResult(shareIntent, 0);
                }
            }
        });




@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;

        if (mSignInClicked) {
            // The user has already clicked 'sign-in' so we attempt to
            // resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();


        }
    }
}



@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub
    mSignInClicked = false;
    // Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
    Intent shareIntent = new PlusShare.Builder(this)
    .setType("text/plain")
    .setText("Welcome to the Google+ platform.")
    .setContentUrl(Uri.parse(articleurl))
    //.setContentUrl(Uri.parse("https://developers.google.com/+/"))
    .getIntent();

    startActivityForResult(shareIntent, 0);


}



@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    //mGoogleApiClient.connect();
}



@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}



@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub
    if(!(mGoogleApiClient.isConnected()))   
        mGoogleApiClient.connect();
}
/**
 * Sign-in into google
 * */
/*private void signInWithGplus() {
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
}
 */
/**
 * Method to resolve any signin errors
 * */
private void resolveSignInError() {
    try {
        if (mConnectionResult.hasResolution()) {

            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        }
    } catch (SendIntentException e) {
        mIntentInProgress = false;
        mGoogleApiClient.connect();
    }
    catch (Exception e) {
        mIntentInProgress = false;
        mGoogleApiClient.connect();
    }

}

首先,请通过以下链接了解GoogleApiClient的实际含义

https://developers.google.com/android/guides/api-client

http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/

https://developers.google.com/+/mobile/android/share/

编辑

不要忘记manifest.xml中获得以下权限

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

适用于iOS的Google Plus API:共享功能-触摸本机对话框共享中的“公共”时崩溃的应用程序?

来自分类Dev

Google Plus共享按钮显示错误的图像

来自分类Dev

来自Google Plus的网址共享

来自分类Dev

Google Plus登录Android吗?

来自分类Dev

整合google plus android错误

来自分类Dev

Android上的Google Plus邀请

来自分类Dev

应用程序未显示在iOS8共享扩展中共享选项的共享菜单中

来自分类Dev

如何在cordova / ionic应用程序中放置Google Plus +1按钮?

来自分类Dev

如何在离子应用程序中共享链接回该应用程序的链接

来自分类Dev

在颤抖中共享首选项停止应用程序

来自分类Dev

如何通过Android应用程序在Google Plus中发布图片和链接网址?

来自分类Dev

MEF中共享的应用程序资源

来自分类Dev

如何在Phonegap for Android应用程序中集成Google Plus Oauth API

来自分类Dev

Android Google Plus登录

来自分类Dev

将Android应用与Google Plus同步

来自分类Dev

从Android应用程序中集成的Google Plus注销

来自分类Dev

在多个Linux发行版中共享应用程序?

来自分类Dev

在我的应用程序上查看Google Plus发布信息

来自分类Dev

如何在iOS中共享时将可点击链接发送到Gmail邮件正文和Google Plus邮件

来自分类Dev

在多个Windows / WP应用程序中共享ResourceDictionary

来自分类Dev

Google Plus共享-图片错误

来自分类Dev

在Qt应用程序中共享Cookie的最佳方法

来自分类Dev

Google Plus和Twitter应用程序在Android上的何处打开链接

来自分类Dev

如何在复合WPF应用程序中共享ResourceDictionaries?

来自分类Dev

在android中共享应用程序功能

来自分类Dev

在android中共享应用程序链接和apk文件

来自分类Dev

通过 google plus ios 登录时应用程序崩溃

来自分类Dev

watsapp 在 Angular 7 Web 应用程序中共享

来自分类Dev

在 MTA XSA 应用程序中共享 HDI 容器

Related 相关文章

  1. 1

    适用于iOS的Google Plus API:共享功能-触摸本机对话框共享中的“公共”时崩溃的应用程序?

  2. 2

    Google Plus共享按钮显示错误的图像

  3. 3

    来自Google Plus的网址共享

  4. 4

    Google Plus登录Android吗?

  5. 5

    整合google plus android错误

  6. 6

    Android上的Google Plus邀请

  7. 7

    应用程序未显示在iOS8共享扩展中共享选项的共享菜单中

  8. 8

    如何在cordova / ionic应用程序中放置Google Plus +1按钮?

  9. 9

    如何在离子应用程序中共享链接回该应用程序的链接

  10. 10

    在颤抖中共享首选项停止应用程序

  11. 11

    如何通过Android应用程序在Google Plus中发布图片和链接网址?

  12. 12

    MEF中共享的应用程序资源

  13. 13

    如何在Phonegap for Android应用程序中集成Google Plus Oauth API

  14. 14

    Android Google Plus登录

  15. 15

    将Android应用与Google Plus同步

  16. 16

    从Android应用程序中集成的Google Plus注销

  17. 17

    在多个Linux发行版中共享应用程序?

  18. 18

    在我的应用程序上查看Google Plus发布信息

  19. 19

    如何在iOS中共享时将可点击链接发送到Gmail邮件正文和Google Plus邮件

  20. 20

    在多个Windows / WP应用程序中共享ResourceDictionary

  21. 21

    Google Plus共享-图片错误

  22. 22

    在Qt应用程序中共享Cookie的最佳方法

  23. 23

    Google Plus和Twitter应用程序在Android上的何处打开链接

  24. 24

    如何在复合WPF应用程序中共享ResourceDictionaries?

  25. 25

    在android中共享应用程序功能

  26. 26

    在android中共享应用程序链接和apk文件

  27. 27

    通过 google plus ios 登录时应用程序崩溃

  28. 28

    watsapp 在 Angular 7 Web 应用程序中共享

  29. 29

    在 MTA XSA 应用程序中共享 HDI 容器

热门标签

归档