Some time ago, the company wants to integrate QQ to share, although we all know Tencent’s documents to write pits, but there is no way, can only be forced to head up.

First of all, we need to download the SDK of QQ. I used the basic version open_sdk_R5788_lite.jar, and then import it into the project. We don’t need to mention this, and then we need to apply for permissions in the manifest file.

Then put it under the Application TAB

<activity
    android:name="com.tencent.tauth.AuthActivity"
    android:noHistory="true"
    android:launchMode="singleTask" >
    <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="tencentAPP_ID" />
    </intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:configChanges="orientation|keyboardHidden|screenSize"
    />
Copy the code

These two activities must be written or they will not succeed,

<data android:scheme="tencentAPP_ID" />
Copy the code

Note here, add Tencent in front of app_id, otherwise you will not receive results after sharing, also don’t know why Tencent did this.

Next create the instance and add it to the code

mTencent = Tencent.createInstance("APP_ID", this.getApplicationContext());
Copy the code

To implement the callback

class MyIUiListener implements IUiListener{ @Override public void onComplete(Object o) { Toast. MakeText (InviteFriendActivity. This, to share success! "" , Toast.LENGTH_SHORT).show(); } @Override public void onError(UiError uiError) { Toast.makeText(InviteFriendActivity.this, uiError.errorMessage, Toast.LENGTH_SHORT).show(); {} @ Override public void onCancel () Toast. MakeText (InviteFriendActivity. This "share!" , Toast.LENGTH_SHORT).show(); }}Copy the code

When accepting shared results, add the following code to the onActivityResult method of the Activity that calls the interface:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (null != mTencent) {
       Tencent.onActivityResultData(requestCode, resultCode, data, listener);
    }
}
Copy the code

The listener in the onActivityResultData interface is the corresponding callback UIListener implemented by the currently invoked Activity.

Finally, I woke up QQ and shared it. At that time, I reported various errors according to QQ documents, but I don’t know why. Later, I checked on the Internet and posted the code to share it

public class ShareUtils {

    /**
     * (2) 分享纯图片
     * QQShare.SHARE_TO_QQ_KEY_TYPE    必选 Int    分享类型,分享纯图片时填写QQShare.SHARE_TO_QQ_TYPE_IMAGE。
     * QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL 必选 String 需要分享的本地图片路径。
     * QQShare.SHARE_TO_QQ_APP_NAME    可选 String 手Q客户端顶部,替换“返回”按钮文字,如果为空,用返回代替。
     * QQShare.SHARE_TO_QQ_EXT_INT 可选 Int    分享额外选项,两种类型可选(默认是不隐藏分享到QZone按钮且不自动打开分享到QZone的对话框):
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN,分享时自动打开分享到QZone的对话框。
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE,分享时隐藏分享到QZone按钮。
     */
    public static  void onClickShare(Tencent mTencent, Activity activity, IUiListener listener) {
        Bundle params = new Bundle();
//        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL,imageUrl.getText().toString());
//        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, appName.getText().toString());
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        mTencent.shareToQQ(activity, params, listener);
    }
    /**
     * (3) 分享音乐
     * QQShare.SHARE_TO_QQ_KEY_TYPE    必填 Int    分享的类型。分享音乐填Tencent.SHARE_TO_QQ_TYPE_AUDIO。
     * QQShare.PARAM_TARGET_URL    必选 String 这条分享消息被好友点击后的跳转URL。
     * QQShare.SHARE_TO_QQ_AUDIO_URL   必填 String 音乐文件的远程链接, 以URL的形式传入, 不支持本地音乐。
     * QQShare.PARAM_TITLE 必选 String 分享的标题, 最长30个字符。
     * QQShare.PARAM_SUMMARY   可选 String 分享的消息摘要,最长40个字符。
     * QQShare.SHARE_TO_QQ_IMAGE_URL   可选 String 分享图片的URL或者本地路径。
     * QQShare.SHARE_TO_QQ_APP_NAME    可选 String 手Q客户端顶部,替换“返回”按钮文字,如果为空,用返回代替。
     * QQShare.SHARE_TO_QQ_EXT_INT 可选 Int    分享额外选项,两种类型可选(默认是不隐藏分享到QZone按钮且不自动打开分享到QZone的对话框):
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN,分享时自动打开分享到QZone的对话框。
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE,分享时隐藏分享到QZone按钮。
     */
    public static  void onClickAudioShareImage(Tencent mTencent, Activity activity, IUiListener listener) {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_AUDIO);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的标题");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "要分享的摘要");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  "http://www.baidu.com");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://gaopin-img.bj.bcebos.com/chinacity.jpg");
        params.putString(QQShare.SHARE_TO_QQ_AUDIO_URL, "音乐链接");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "测试应用222222");
        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        mTencent.shareToQQ(activity, params, listener);
    }
    /**
     * (4) 分享应用
     * QQShare.SHARE_TO_QQ_KEY_TYPE    必填 Int    分享的类型。分享音乐填Tencent.SHARE_TO_QQ_TYPE_PP。
     * QQShare.PARAM_TITLE 必选 String 分享的标题, 最长30个字符。
     * QQShare.PARAM_SUMMARY   可选 String 分享的消息摘要,最长40个字符。
     * QQShare.SHARE_TO_QQ_IMAGE_URL   可选 String 分享图片的URL或者本地路径。
     * QQShare.SHARE_TO_QQ_APP_NAME    可选 String 手Q客户端顶部,替换“返回”按钮文字,如果为空,用返回代替。
     * QQShare.SHARE_TO_QQ_EXT_INT 可选 Int    分享额外选项,两种类型可选(默认是不隐藏分享到QZone按钮且不自动打开分享到QZone的对话框):
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN,分享时自动打开分享到QZone的对话框。
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE,分享时隐藏分享到QZone按钮。
     */
    public static  void onClickAppShare(Tencent mTencent, Activity activity, IUiListener listener) {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_APP);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的标题");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "要分享的摘要");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://gaopin-img.bj.bcebos.com/chinacity.jpg");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "测试应用");
        mTencent.shareToQQ(activity, params, listener);
    }

    /**
     *  1) 分享图文消息
     * QQShare.SHARE_TO_QQ_KEY_TYPE    必填 Int    分享的类型。图文分享(普通分享)填Tencent.SHARE_TO_QQ_TYPE_DEFAULT
     * QQShare.PARAM_TARGET_URL    必填 String 这条分享消息被好友点击后的跳转URL。
     * QQShare.PARAM_TITLE 必填 String 分享的标题, 最长30个字符。
     * QQShare.PARAM_SUMMARY   可选 String 分享的消息摘要,最长40个字。
     * QQShare.SHARE_TO_QQ_IMAGE_URL   可选 String 分享图片的URL或者本地路径
     * QQShare.SHARE_TO_QQ_APP_NAME    可选 String 手Q客户端顶部,替换“返回”按钮文字,如果为空,用返回代替
     * QQShare.SHARE_TO_QQ_EXT_INT 可选 Int    分享额外选项,两种类型可选(默认是不隐藏分享到QZone按钮且不自动打开分享到QZone的对话框):
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN,分享时自动打开分享到QZone的对话框。
     * QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE,分享时隐藏分享到QZone按钮
     */
    public static void shareToQQ(Tencent mTencent, Activity activity, IUiListener listener) {
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "标题");// 标题
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要");// 摘要
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"http://www.baidu.com");// 内容地址
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://gaopin-img.bj.bcebos.com/chinacity.jpg");// 网络图片地址  params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "应用名称");// 应用名称
        params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "其它附加功能");
        // 分享操作要在主线程中完成
        mTencent.shareToQQ(activity, params, listener);
    }
    /**
     * 1.14 分享到QQ空间
     * QzoneShare.SHARE_TO_QZONE_KEY_TYPE  选填 Int    SHARE_TO_QZONE_TYPE_IMAGE_TEXT(图文)
     * QzoneShare.SHARE_TO_QQ_TITLE    必填    分享的标题,最多200个字符。
     * QzoneShare.SHARE_TO_QQ_SUMMARY  选填 String 分享的摘要,最多600字符。
     * QzoneShare.SHARE_TO_QQ_TARGET_URL   必填 String 需要跳转的链接,URL字符串。
     * QzoneShare.SHARE_TO_QQ_IMAGE_URL    选填 String 分享的图片, 以ArrayList<String>的类型传入,以便支持多张图片(注:图片最多支持9张图片,多余的图片会被丢弃)
     */
    public static void shareToQZone(Tencent mTencent, Activity activity, IUiListener listener) {
        Bundle params = new Bundle();
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "标题");// 标题
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要");// 摘要
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,"http://www.baidu.com");// 内容地址
        ArrayList<String> imgUrlList = new ArrayList<>();
        imgUrlList.add("http://gaopin-img.bj.bcebos.com/chinacity.jpg");
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imgUrlList);// 图片地址
        // 分享操作要在主线程中完成

        mTencent.shareToQzone(activity, params,listener);
    }
}
Copy the code

If you do not add http:// to the url, you will be prompted with a tagURL error.

OK, if you see any questions there you can ask them