将应用的网页,图片,问题分享到qq好友和请求空间中,是很常见的开发需求,首先是分享到qq中
一、到腾讯公共平台注册相关应用的信息,拿到应用的app_id等相关信息
二、下载官网中的相关的demo,导入相关的库
三、代码实现
分享到qq好友
首先在文件清单中注册分享需要的相关信息
<!--分享到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" />
<!--在官网上申请的app_id记住前面一定要加上tencent-->
<data android:scheme="tencent*******"/>
</intent-filter>
</activity>
<!--分享qq时回调时需要-->
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
实例化分享类
Tencent mTencent = Tencent.createInstance(QQ_APP_ID, mContext);
public void shareQQ() {
final 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, shareTitle);
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareContext);
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareIcon);
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, APP_NAME);
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, 0x00);
mTencent.shareToQQ((Activity) mContext, params, qqShareListener);
}
在activity中设置分享回调
/**
* QQ和QQ空间分享回调
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_QZONE_SHARE) {
Tencent.onActivityResultData(requestCode, resultCode, data, ShareUtil.qqZoneShareListener);
}else if (requestCode == Constants.REQUEST_QQ_SHARE) {
Tencent.onActivityResultData(requestCode, resultCode, data, ShareUtil.qqShareListener);
}
}
/**
* 分享到QQ回调结果
*/
public static IUiListener qqShareListener = new IUiListener() {
@Override
public void onCancel() {
Toast.makeText(mContext, "QQ分享取消", Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Object response) {
Toast.makeText(mContext, "QQ分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
Toast.makeText(mContext, "QQ分享错误", Toast.LENGTH_SHORT).show();
}
};
分享到请求QQ空间中
/**
* 分享QQ空间
*/
public void shareToQzone () {
final 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, shareTitle);
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareContext);
params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareIcon);
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, new ArrayList<String>());//这个参数必须要有(官网上是不正确的),不能缺省,即使没有图片时,也要有这个,否则分享失败
mTencent.shareToQzone((Activity)mContext, params, qqZoneShareListener);
}
/
* 官方推荐 用异步方式启动分享
* @param params
*/
private void doShareToQzone(final Bundle params) {
// QZone分享要在主线程做
ThreadManager.getMainHandler().post(new Runnable() {
@Override
public void run() {
if (null != mTencent) {
mTencent.shareToQzone((Activity)mContext, params, qqZoneShareListener);
}
}
});
}
/**
* 分享到QQ空间回调结果
*/
public static IUiListener qqZoneShareListener = new IUiListener() {
@Override
public void onCancel() {
Toast.makeText(mContext, "QQ空间分享取消", Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Object response) {
Toast.makeText(mContext, "QQ空间分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError uiError) {
Toast.makeText(mContext, "QQ空间分享错误", Toast.LENGTH_SHORT).show();
}
}