分享到QQ踩坑过程——分享之二

分享部分的第二篇——分享给QQ好友

上一篇文章详细介绍了微信分享的一些踩坑过程。这一片主要是分享给QQ好友。

废话不说,先看效果图。

分享到QQ踩坑过程——分享之二_第1张图片

  • 腾讯开放平台注册开发者账号
  • 配置Android Studio工程
  • 调用API/封装业务代码
  • 调用实现
  • 其他注意事项

注册开发者账号并获取APP_ID

分享到QQ踩坑过程——分享之二_第2张图片

在腾讯开放平台中,按要求注册账户,登录,进入管理中心,然后“创建应用
分享到QQ踩坑过程——分享之二_第3张图片

创建应用通过后,获得其中的APP ID, secretKey等重要字符串。

配置Android Studio工程

1.下载jar文件
给个链接给你:下载地址

2.在工程根目录下的build.gradle文件中配置

dependencies {
    // 分享给QQ好友
    compile files('libs/open_sdk_r5923_lite.jar')
}

3.在Android Menifest.xml 文件中配置若干需要的权限

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

4.在application标签下添加

注意其中填写app id的地方是 字符串 tencent 再拼接上你的 app id。

<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="tencent你的APP ID" />
    intent-filter>
activity>
<activity
    android:name="com.tencent.connect.common.AssistActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

封装

package com.weshare.voice.du.lock.controller;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.weshare.voice.du.lock.utils.LogUtil;
import com.weshare.voice.du.lock.utils.ToastUtil;

/**
 * date: 2017/12/21
 * description:  QQShareComponent.java
 * author: 沈煜 
 * blog: http://blog.csdn.net/shenyu_njau
 */

public class QQShareComponent {
    private static final String APP_ID = "你的APP ID";
    private static final String TAG = "QQShareComponent";

    private static QQShareComponent mInstance;
    private Tencent mTencent;
    private static Bundle mBundle;
    private static Activity mTargetActivity;
    private static IUiListener mIUiListener;
    private static String mTitle;
    private static String mDetailUrl;
    private static String mImageUrl;
    private static String mSummary;
    private static String mAppSource;

    private QQShareComponent(Context mContext) {
        // Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
        // 其中APP_ID是分配给第三方应用的appid,类型为String。
        try {
            mTencent = Tencent.createInstance(APP_ID, mContext.getApplicationContext());
            LogUtil.d("mTencent=" + mTencent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static QQShareComponent createQQShareInstance(Context mContext) {
        if (mInstance == null) {
            mInstance = new QQShareComponent(mContext);
        }
        mBundle = mInstance.setShareData();
        return mInstance;
    }

    private Bundle setShareData() {
        if (TextUtils.isEmpty(mDetailUrl) && TextUtils.isEmpty(mImageUrl) && TextUtils.isEmpty(mSummary)) {
            ToastUtil.showShortToastCenter("请设置要分享的内容");
            return null;
        }
        Bundle bundle = new Bundle();
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);

        //这条分享消息被好友点击后的跳转URL。
        //bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  "要分享的Url");
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, mDetailUrl);

        //分享的标题。注:SHARE_TO_QQ_TARGET_URL、SHARE_TO_QQ_IMAGE_URL、SHARE_TO_QQ_SUMMARY,最少必须有一个是有值的。
        //bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的标题");
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, mTitle);

        //分享的图片URL
        //bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"缩略图");
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, mImageUrl);

        //分享的消息摘要,最长50个字
        //bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY,  "要分享的摘要");
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, mSummary);

        //标识该消息的来源应用,值为应用名称+AppId。
        //bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "测试应用+APP_ID");
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, mAppSource);

        return bundle;
    }

    public static class Builder {
        private Context mContext;

        /**
         * QQ分享控件构造器对象的构造方法
         * @param mContext 上下问对象
         */
        public Builder(Context mContext) {
            LogUtil.setLogTag(TAG);
            this.mContext = mContext;
        }

        /**
         * 设置需要分享的目标Activity
         * @param activity 目标Activity
         * @return 返回该构造器对象
         */
        public Builder shareTarget(Activity activity) {
            QQShareComponent.mTargetActivity = activity;
            return this;
        }

        /**
         * 设置分享内容的标题
         * @param title 内容标题
         * @return 返回该构造器对象
         */
        public Builder setShareContentTitle(String title) {
            QQShareComponent.mTitle = title;
            return this;
        }

        /**
         * 设置分享内容指向的URL
         * @param url url
         * @return 返回该构造器对象
         */
        public Builder setShareContentUrl(String url) {
            QQShareComponent.mDetailUrl = url;
            return this;
        }

        /**
         * 设置分享缩略图的URL
         * @param url url
         * @return 返回该构造器对象
         */
        public Builder setShareContentImageUrl(String url) {
            QQShareComponent.mImageUrl = url;
            return this;
        }

        /**
         * 设置分享内容的摘要
         * @param summary 摘要
         * @return 返回该构造器对象
         */
        public Builder setShareContentSummary(String summary) {
            QQShareComponent.mSummary = summary;
            return this;
        }

        /**
         * 设置分享内容的来源
         * @param appName 来源app名字
         * @return 返回该构造器对象
         */
        public Builder setShareAppSource(String appName) {
            QQShareComponent.mAppSource = appName + APP_ID;
            return this;
        }

        /**
         * 设置分享UI回调接口
         * @param uiListener ui回调接口
         * @return 返回改构造器对象
         */
        public Builder setUiListener(IUiListener uiListener) {
            QQShareComponent.mIUiListener = uiListener;
            LogUtil.d("mIUiListener=" + mIUiListener);
            return this;
        }

        /**
         * 创建QQ分享组件对象,一个单例对象
         * @return 返回QQ分享对象
         */
        public QQShareComponent build() {
            return QQShareComponent.createQQShareInstance(mContext);
        }
    }

    /**
     * 在分享的Activity中,
     * 必须要重写
     * @see Activity#onActivityResult(int, int, Intent)
     * 并且在该方法中调用
     * @see #enableListener(int, int, Intent)
     *
     * 
否则回调接口无效。
* * @param requestCode The integer request code originally supplied to * startActivityForResult(), allowing you to identify who this * result came from. * @param resultCode The integer result code returned by the child activity * through its setResult(). * @param data An Intent, which can return result data to the caller * (various data can be attached to Intent "extras"). */
@SuppressWarnings("deprecation") public void enableListener(int requestCode, int resultCode, Intent data) { LogUtil.d("mTencent=" + mTencent); if (mTargetActivity != null && mIUiListener != null) { if (mTencent != null) { mTencent.onActivityResult(requestCode, resultCode, data); } } } /** * 开始分享 */ public void share() { LogUtil.d("mTencent=" + mTencent); if (mTargetActivity != null && mBundle != null && mIUiListener != null) if (mTencent != null) { mTencent.shareToQQ(mTargetActivity, mBundle, mIUiListener); } else { mTencent = Tencent.createInstance(APP_ID, mTargetActivity); share(); } } }

封装说明,在各方法中均有注释

使用

QQShareComponent.Builder builder = new QQShareComponent.Builder(this)
                        .shareTarget(this)
                        .setShareContentTitle(mShareAppStr)
                        .setShareContentUrl(Constants.Url.SHARE_APP_URL)
                        .setShareContentSummary(mShareSummary)
                        .setShareContentImageUrl(Constants.Url.SHARE_ICON)
                        .setShareAppSource(mShareAppStr)
                        .setUiListener(this);
qqShareComponent = builder.build();
qqShareComponent.share();

—————————————————————不怎么华丽的分割线——————————————————

你可能感兴趣的:(Android)