分享在每个App应用中是很常见的,今天就来实现一下接入QQ提供的第三方分享。
1.使用QQ分享时记得先到我们的腾讯开放平台注册账号然后获取开发者资格地址:http://open.qq.com/ 创建应用获取AppId和AppKey,具体请看我的这篇博客:http://blog.csdn.net/sandyran/article/details/53319846
然后下载SDKJar地址:http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD
2.接下来打开我们的AndroidStudio新建一个项目把刚才下载的SDK放入到libs中然后添加依赖
结构如下:
3.开启我们的集成之路吧打开AndroidManifest在里面添加权限和注册SDKActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shiran.qqsharedemo">
<!-- QQ分享所需权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册SDKActivity -->
<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="tencentID1105602574" /> <!-- 获取的APPID -->
</intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize" />
</application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
打开布局文件在里面放入三个Button按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shiran.qqsharedemo.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QQ分享"
android:onClick="qqShare"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QQ空间分享"
android:onClick="qqQzoneShare"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="分享音乐"
android:onClick="qqMusicShare"/>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
MainActivity代码如下
package com.shiran.qqsharedemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String APP_ID = "1105602574"; //获取的APPID
private ShareUiListener mIUiListener;
private Tencent mTencent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//传入参数APPID
mTencent = Tencent.createInstance(APP_ID, MainActivity.this.getApplicationContext());
}
/**
* 分享到QQ
* @param v
*/
public void qqShare(View v) {
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, "然了个然CSDN博客");//分享标题
params.putString(QQShare.SHARE_TO_QQ_SUMMARY,"不管是怎样的过程,最终目的还是那个理想的结果。");//要分享的内容摘要
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"http://blog.csdn.net/sandyran");//内容地址
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://avatar.csdn.net/B/3/F/1_sandyran.jpg");//分享的图片URL
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "测试");//应用名称
mTencent.shareToQQ(MainActivity.this, params, new ShareUiListener());
}
/**
* 分享到QQ空间
* @param v
*/
public void qqQzoneShare(View v) {
int QzoneType = QzoneShare.SHARE_TO_QZONE_TYPE_NO_TYPE;
Bundle params = new Bundle();
params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneType);
params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "然了个然CSDN博客");//分享标题
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "不管是怎样的过程,最终目的还是那个理想的结果。");//分享的内容摘要
params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "http://blog.csdn.net/sandyran/article/details/53204529");//分享的链接
//分享的图片, 以ArrayList<String>的类型传入,以便支持多张图片(注:图片最多支持9张图片,多余的图片会被丢弃)
ArrayList<String> imageUrls = new ArrayList<String>();
imageUrls.add("http://avatar.csdn.net/B/3/F/1_sandyran.jpg");//添加一个图片地址
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);//分享的图片URL
mTencent.shareToQzone(MainActivity.this, params, new ShareUiListener());
}
/**
* 分享音乐
* @param v
*/
public void qqMusicShare(View v) {
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, "歌曲名:说穿");//分享的标题
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "专辑名:最好的我们,歌手:谢楠");//分享的内容摘要
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "https://y.qq.com/n/yqq/song/004Dle9I3TaSai.html");//分享的链接
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://y.gtimg.cn/music/photo_new/T002R300x300M000000kbocv24CRbE.jpg?max_age=2592000");//分享的图片URL
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "测试");//应用名称
params.putString(QQShare.SHARE_TO_QQ_EXT_INT, String.valueOf(QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN));//分享时自动打开分享到QZone的对话框
mTencent.shareToQQ(MainActivity.this, params, new ShareUiListener());
}
/**
* 自定义监听器实现IUiListener,需要3个方法
* onComplete完成 onError错误 onCancel取消
*/
private class ShareUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
//分享成功
}
@Override
public void onError(UiError uiError) {
//分享失败
}
@Override
public void onCancel() {
//分享取消
}
}
/**
* 回调
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (null != mTencent) {
mTencent.onActivityResult(requestCode, resultCode, data);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
以上是接入QQ分享的全部代码,并以成功接入QQ分享用你的手机去测试一下吧。