Android APP integrates Sina Weibo sharing function

  • 2021-10-25 07:46:03
  • OfStack

This article shares the integration of Sina Weibo sharing functions for your reference. The specific contents are as follows

Download Sina Weibo Android SDK

Direct import weibosdkcore. jar: For projects that only require authorization, sharing, and network request framework functionality.
Whichever method you use, you need to copy all the corresponding libweibosdkcore. so file directories under lib directory in demo to Demo in your target project. In app > src > Create a new folder jniLibs in main, copy all the corresponding libweibosdkcore. so file directories under lib directory in demo, and do not change any file and folder location.

When sharing, refer to WBShareMainActivity in demo. Here is the entrance to sharing. The main code is as follows:


//  Create Weibo  SDK  Interface instance 
    mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(mContext, SysConstants.SHARE_WEIBO_APP_ID);

    //  Register on Sina Weibo 
    mWeiboShareAPI.registerApp();
    Intent i = new Intent(mContext, WBShareActivity.class);
    i.putExtra(WBShareActivity.KEY_SHARE_TYPE, WBShareActivity.SHARE_ALL_IN_ONE);
    i.putExtra(WBShareActivity.IMAGE_URL, imgUrl);
    mContext.startActivity(i);

Before sharing, you need to replace parameters such as APP_KEY with your own applied parameters. For parameters, please refer to Constants class in Demo.

Before sharing Weibo, it is necessary to declare the corresponding Action: ACTION_SDK_REQ_ACTIVITY in AndroidManifest.xml and Activity (class that evokes Weibo main program) that needs to receive messages, as follows:


<activity
  android:name="com.sina.weibo.sdk.demo.WBShareActivity"
  android:configChanges="keyboardHidden|orientation"
  android:screenOrientation="portrait" >
  <intent-filter>
      <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity 
   android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser" 
   android:configChanges="keyboardHidden|orientation"
   android:windowSoftInputMode="adjustResize"
   android:exported="false" >
</activity>

The sharing function is mainly realized by WBShareActivity, including text, pictures, web pages, music, video, sound, specific code reference Demo.

WBShareActivity implements IWeiboHandler # Response interface to receive the data returned by Weibo after sharing. The code is as follows:


/**
   *  Receiving data requested by the micro-client blog. 
   *  When the Weibo client evokes the current application and shares it, the method is called. 
   * 
   * @param baseRequest  Weibo request data object 
   * @see {@link IWeiboShareAPI#handleWeiboRequest}
   */
  @Override
  public void onResponse(BaseResponse baseResp) {
    if(baseResp!= null){
      switch (baseResp.errCode) {
      case WBConstants.ErrorCode.ERR_OK:
        Toast.makeText(this, R.string.weibosdk_demo_toast_share_success, Toast.LENGTH_LONG).show();
        break;
      case WBConstants.ErrorCode.ERR_CANCEL:
        Toast.makeText(this, R.string.weibosdk_demo_toast_share_canceled, Toast.LENGTH_LONG).show();
        break;
      case WBConstants.ErrorCode.ERR_FAIL:
        Toast.makeText(this, 
            getString(R.string.weibosdk_demo_toast_share_failed) + "Error Message: " + baseResp.errMsg, 
            Toast.LENGTH_LONG).show();
        break;
      }
    }
  }

It should be noted that in getImageObj, the method of sharing pictures in WBShareActivity, the comment says that the thumbnail set is not the picture at the time of sharing. We just need to share the pictures in imageObject, there is no 32K size limit. If we put thumbnails in imageObject, the shared pictures are thumbnails and cannot be seen clearly.


/**
   *  Create a picture message object. 
   * 
   * @return  Picture message object. 
   */
  private ImageObject getImageObj() {
    ImageObject imageObject = new ImageObject();
    BitmapDrawable bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();
    // Set thumbnails.   Note: The final compressed thumbnail size must not exceed  32kb . 
    /* The note above is demo But in fact, there is no need to set thumbnails here, just set the pictures we share directly to imageObjet Can be inside */
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_logo);
    imageObject.setImageObject(bitmap);
    return imageObject;
  }

For other matters, please refer to the documents provided by Sina Weibo sdk.


Related articles: