Implementation code of Android alliance third-party login and sharing

preface

Recently, the project once again needs to integrate the three-party login and sharing of Youmeng, which has not been recorded before, so let's write this time

preparation

1. Register Youmeng account to create an application and obtain key: application address http://www.umeng.com

2. Apply for a third-party account for the corresponding platform (Tencent, wechat, Sina, etc.) and obtain the key and password

Integration steps

Because sharesdk sharing is similar to the third-party login integration (sharing jar packages), I'll talk about it together.

1. Download the SDK shared by sharesdk

Download address: https://developer.umeng.com/sdk

2. Import jar and res

Open the development documentation and download the corresponding SDK and demo. Select the functions you need when downloading the SDK

The corresponding functions of the downloaded files are shown in the figure below:

Import into Android studio and add dependencies

3. Configure Android manifest XML

Activity required in SDK

Sina

  <activity
    android:name="com.umeng.socialize.media.WBShareCallBackActivity"
    android:configChanges="keyboardHidden|orientation"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:exported="false"
    >
  </activity>
  <activity android:name="com.sina.weibo.sdk.web.WeiboSdkWebActivity"
       android:configChanges="keyboardHidden|orientation"
       android:exported="false"
       android:windowSoftInputMode="adjustResize"
  >

  </activity>
  <activity
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
    android:launchMode="singleTask"
    android:name="com.sina.weibo.sdk.share.WbShareTransActivity">
    <intent-filter>
      <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

  </activity>

WeChat

 <activity
      android:name=".wxapi.WXEntryActivity"
      android:configChanges="keyboardHidden|orientation|screenSize"
      android:exported="true"
      android:theme="@android:style/Theme.Translucent.NoTitleBar" />

QQ

   <activity
    android:name="com.tencent.tauth.AuthActivity"
    android:launchMode="singleTask"
    android:noHistory="true" >
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data android:scheme="tencent100424468" />
    </intent-filter>
    </activity>
    <activity
    android:name="com.tencent.connect.common.AssistActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:configChanges="orientation|keyboardHidden|screenSize"/>

Add permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Adaptation

Android 6.0 permission adaptation

if(Build.VERSION.SDK_INT>=23){
         String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYstem_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
         ActivityCompat.requestPermissions(this,mPermissionList,123);
       }

123 is the requestcode. You can judge whether the user agrees to the authorization according to this code. If you do not agree, you can handle it according to the callback:

@Override
public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults) {

}

Initialization settings

@Override
  public void onCreate() {
    super.onCreate();
    UMConfigure.init(this,"5a12384aa40fa3551f0001d1","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0

  }

Next, you need to set the appkey of each platform:

    PlatformConfig.setWeixin("wxdc1e388c3822c80b","3baf1193c85774b3fd9d18447d76cab0");
    //豆瓣RENREN平台目前只能在服务器端配置
    PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com");
    PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
    PlatformConfig.setQQZone("100424468","c7394704798a158208a74ab60104f0ba");
    PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi","MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
    PlatformConfig.setAlipay("2015111700822536");
    PlatformConfig.setLaiwang("laiwangd497e70d4","d497e70d4c3e4efeab1381476bac4c5e");
    PlatformConfig.setPinterest("1439206");
    PlatformConfig.setKakao("e4f60e065048eb031e235c806b31c70f");
    PlatformConfig.setDing("dingoalmlnohc0wggfedpk");
    PlatformConfig.setVKontakte("5764965","5My6SNliAaLxEm3Lyd9J");
    PlatformConfig.setDrop@R_984_2419@("oz8v5apet3arcdy","h7p2pjbzkkxt02a");
    PlatformConfig.setYnote("9c82bf470cba7bd2f1819b0ee26f86c6ce670e9b");

autograph

You must use the APK signed by the keystore to test. If the signature file is not added, the authorization of some platforms will be affected.

Use steps

1、 Share

There are two forms of alliance sharing:

Using the sharing of the sharing panel, users can call our method of opening the sharing panel and click the corresponding platform of the sharing panel to share.

Without sharing the shared panels, users can write their own share buttons or trigger events, and then call our sharing methods to share. In short, direct sharing is to insert sharing behavior into the user's own interface component. The sharing panel is to open an interface component we have written and share according to the click event.

The code to open the sharing panel is as follows:

new ShareAction(MainActivity.this)
.setPlatform(SHARE_MEDIA.QQ)//传入平台
.withText("hello")//分享内容
.setCallback(umShareListener)//回调监听器
.share();

There are many types of sharing. For details, see the developer documentation of Youmeng. Here, take the connection as an example:

Sharing links can be shared using umweb:

UMWeb web = new UMWeb(Defaultcontent.url);
    web.setTitle("This is music title");//标题
    web.setThumb(thumb); //缩略图
    web.setDescription("my description");//描述

Then the call is set to this parameter in ShareAction:

new ShareAction(ShareActivity.this)
  .withMedia(web)
  .share();

2、 Third party login

There are two usage scenarios for third-party login:

An authorized interface for obtaining user data. Using this interface, you can obtain all data (including name, gender, avatar, etc.) returned by the third-party platform. At present, the platforms supporting third-party login include:

Domestic platforms (wechat, Sina Weibo, QQ, Douban, Renren)

Foreign platforms (Facebook, twitter, LinkedIn, kakao, Vkontakte Drop@R_984_2419 @)

It is recommended to use the first method directly here, because in essence, the user data of the three-party platform needs to be pulled for the three-party login. From this point of view, the security of directly calling the SDK is the same as that of requesting through the background server

There are two usage scenarios for third-party login:

An authorized interface for obtaining user data. Using this interface, you can obtain all data (including name, gender, avatar, etc.) returned by the third-party platform. At present, the platforms supporting third-party login include:

Domestic platforms (wechat, Sina Weibo, QQ, Douban, Renren)

Foreign platforms (Facebook, twitter, LinkedIn, kakao, Vkontakte Drop@R_984_2419 @)

It is recommended to use the first method directly here, because in essence, the user data of the three-party platform needs to be pulled for the three-party login. From this point of view, the security of directly calling the SDK is the same as that of requesting through the background server

Get user information

The following interfaces can be used to obtain user data:

mShareAPI.getPlatformInfo(UserinfoActivity.this,SHARE_MEDIA.SINA,umAuthListener);

Where umauthlistener is the authorization callback, which is constructed as follows:

UMAuthListener authListener = new UMAuthListener() {
    /**
     * @desc 授权开始的回调
     * @param platform 平台名称
     */
    @Override
    public void onStart(SHARE_MEDIA platform) {

    }

    /**
     * @desc 授权成功的回调
     * @param platform 平台名称
     * @param action 行为序号,开发者用不上
     * @param data 用户资料返回
     */
    @Override
    public void onComplete(SHARE_MEDIA platform,int action,Map<String,String> data) {

      Toast.makeText(mContext,"成功了",Toast.LENGTH_LONG).show();

    }

    /**
     * @desc 授权失败的回调
     * @param platform 平台名称
     * @param action 行为序号,开发者用不上
     * @param t 错误原因
     */
    @Override
    public void onError(SHARE_MEDIA platform,Throwable t) {

      Toast.makeText(mContext,"失败:" + t.getMessage(),Toast.LENGTH_LONG).show();
    }

    /**
     * @desc 授权取消的回调
     * @param platform 平台名称
     * @param action 行为序号,开发者用不上
     */
    @Override
    public void onCancel(SHARE_MEDIA platform,int action) {
      Toast.makeText(mContext,"取消了",Toast.LENGTH_LONG).show();
    }
  }; 

After successful login, the third-party platform will return the user data, all of which will be returned in map data.

There are other functions. You can view the development documents of Youmeng. Address:

https://developer.umeng.com/docs/66632/detail/66639

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>