分享到QQ踩坑過程——分享之二
阿新 • • 發佈:2019-01-02
分享部分的第二篇——分享給QQ好友
上一篇文章詳細介紹了微信分享的一些踩坑過程。這一片主要是分享給QQ好友。
廢話不說,先看效果圖。
- 配置Android Studio工程
- 呼叫API/封裝業務程式碼
- 呼叫實現
- 其他注意事項
註冊開發者賬號並獲取APP_ID
在騰訊開放平臺中,按要求註冊賬戶,登入,進入管理中心,然後“建立應用”
建立應用通過後,獲得其中的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: 沈煜 <[email protected]>
* 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)
*
* <br>否則回撥介面無效。</br>
*
* @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();
—————————————————————不怎麼華麗的分割線——————————————————