Android 第三方登录、分享(ShareSDK、友盟)
为下边的项目做准备,写一个第三方登录、分享的demo。分别使用sharesdk和友盟来实现。
先说一下我对两者的使用上的感觉,个人感觉sharesdk比友盟更好一点,好在哪里呢?好在人工服务上。在集成的过程中遇到了各种问题,但是sharesdk的人工服务做的很好,能给答疑解惑,提供解决问题的方法!
接下来上代码(本文使用android studio开发,sharesdk版本v2.7.7,友盟版本v6.0.0):
一、使用sharesdk(sharesdk版本v2.7.7)来集成:
1、要去官网:http://www.mob.com/ 注册账号、添加应用、下载sdk、分别去新浪、微信、QQ开放平台注册应用,拿到key。准备好这些,就让咱们开始吧!
2、在清单文件中添加如下权限:
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<!-- 蓝牙分享所需的权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
3、继续添加Activity的信息,此处注意tencent后面的appid要保持和您配置的QQ的appid一致:
<!-- 调用QQ,需要注册的回调activity -->
<activity
android:name="com.mob.tools.MobUIShell"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize" >
<intent-filter>
<data android:scheme="tencent100371282" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- 调用新浪原生SDK,需要注册的回调activity -->
<intent-filter>
<action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- 调用微信,需要注册的回调activity -->
<!--微信分享回调 -->
<activity android:name=".wxapi.WXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true" android:screenOrientation="portrait" />
4、去assets目录下的ShareSdk.xml目录下修改准备好的QQ、微信、微博的key。 在此处注意:
4.1、assets文件夹在main的根目录下。
4.2、QQ和QZone使用的同一个AppId、AppKey
4.3、微信和朋友圈、收藏使用同一个 AppId、AppSecret
4.4、微博除去配置AppKey、AppSecret之外,还需要配置 RedirectUrl="http://sns.whalecloud.com/sina2/callback" RedirectUrl的值和微博应用信息处的授权回调页保持一致
5、去添加微信的回调
5.1、在包名的下边建立一个名字为“wxapi”的包
5.2、把demo中的“WXEntryActivity”放进去,注意,这个类不需要动,不需要手动调用!!
6、去写代码吧
提前准备PublicStaticData.java:
package com.example.lql.sharesdk.utils;
import com.umeng.socialize.UMShareAPI;
import cn.sharesdk.framework.ShareSDK;
public class PublicStaticData {
public static ShareSDK myShareSDK;
public static UMShareAPI mShareAPI;
}
6.0初始化
PublicStaticData.myShareSDK= new ShareSDK();
PublicStaticData.myShareSDK.initSDK(getApplicationContext());
6.1、打开分享面板
/**
* @param title 标题
* @param text 内容
* @param picurl 图片链接 *
* QQ和QQ空间设置分享链接使用setTitleUrl();
* 设置标题:setTitle
* 设置内容:setText
* 设置网络图片:oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
* 设置本地图片: //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片 *
* 微信 * url仅在微信(包括好友和朋友圈)中使用 * oks.setUrl("http://qq.com"); */
private void showShare(String title,String text,String picurl) {
OnekeyShare oks = new OnekeyShare(); //关闭sso授权
oks.disableSSOWhenAuthorize();
oks.setTitle(title);
oks.setText(text);
if(picurl!=null){
oks.setImageUrl(picurl);
}
// 启动分享
GUI oks.show(this);
}
6.2、分享到指定平台(此处使用微信和微博为例)
/** * 微信 在微信分享中,需要设置setShareType属性,此处应注意
* @param title
* @param content
* @param PicUrl
* @param titleUrl
* Platform.SHARE_TEXT(分享文本),
Platform.SHARE_IMAGE(分享图片),
Platform.SHARE_WEBPAGE(分享网页,既图文分享),
Platform.SHARE_MUSIC(分享音频),
Platform.SHARE_VIDEO(分享视频),
Platform.SHARE_APPS(分享应用,仅微信支持),
Platform.SHARE_FILE(分享文件,仅微信支持)
Platform.SHARE_EMOJI(分享表情,仅微信支持)
*/
public static void shareWX(String title,String content,String PicUrl,String titleUrl){
type="share";
Wechat.ShareParams sp=new Wechat.ShareParams();
sp.setTitle(title);
sp.setText(content);
if(titleUrl!=null){
sp.setTitleUrl(titleUrl); // 标题的超链接
}
if(PicUrl!=null){
sp.setImageUrl(PicUrl);// 图片地址
}
sp.setShareType(Platform.SHARE_IMAGE);
sp.setUrl("http://qq.com");
Platform wx = PublicStaticData.myShareSDK.getPlatform (Wechat.NAME);
wx. setPlatformActionListener (mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
wx.share(sp);
}
/** * 分享到微博
* @param text 文本内容
* @param picUrl 网络图片 (文档中说明分享图片需要审核通过,测试中体现不需要)
*/
public static void shareSina(String text,String picUrl){
type="share";
SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
sp.setText(text);
if(picUrl!=null){
sp.setImageUrl(picUrl);
}
Platform weibo = PublicStaticData.myShareSDK.getPlatform(SinaWeibo.NAME);
weibo.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
weibo.share(sp);
}
6.3第三方登录
/** * 登录
* @param name 登录方式(QQ.NAME、Wechat.NAME、SinaWeibo.NAME)
*/
public static void Login(String name){
type="login";
Platform mPlatform = ShareSDK.getPlatform(name);
mPlatform.setPlatformActionListener(mPlatformActionListener);
mPlatform.authorize();//单独授权,OnComplete返回的hashmap是空的
mPlatform.showUser(null);//授权并获取用户信息
}
6.4登录和分享回调(此处分享和登录使用的一个监听)
public static PlatformActionListener mPlatformActionListener= new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
if(type.equals("login")){
Log.e("onComplete","登录成功");
Log.e("openid",platform.getDb().getUserId()); //拿到登录后的
openid Log.e("username",platform.getDb().getUserName()); //拿到登录用户的昵称
}else{
Log.e("onComplete","分享成功");
}
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Log.e("onError",throwable.toString()+"");
if(type.equals("login")){
Log.e("onError","登录失败"+throwable.toString());
}else{
Log.e("onError","分享失败"+throwable.toString());
}
}
@Override
public void onCancel(Platform platform, int i) {
if(type.equals("login")){
Log.e("onCancel","登录取消");
}else{
Log.e("onCancel","分享取消");
}
}
}
7、至此使用ShareSDK集成常见分三种登录分享方式就完成了,整个过程没有遇到严重的问题,整体体验效果还不错。上整体代码ShareSDKUtils.java:
package com.example.lql.sharesdk.utils;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
/**
* 作者:dell or Xiaomi Li
* 时间: 2018/1/25
* 内容:把回调放在activity里边,就能直接拿到结果过处理了
* 最后修改:
*/
public class ShareSDKUtils {
public static int SHARETYPE = 0X1;
public static int LOGINTYPE = 0X2;
/**
* 分享到微博
*
* @param text 文本内容
* @param picUrl 网络图片 (通过审核后才能添加)
*/
public static void shareSina(String text, String picUrl, PlatformActionListener mPlatformActionListener) {
SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
sp.setText(text);
if (picUrl != null) {
sp.setImageUrl(picUrl);
}
Platform weibo = PublicStaticData.myShareSDK.getPlatform(SinaWeibo.NAME);
weibo.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
weibo.share(sp);
}
/**
* 分享到QQ空间
*
* @param title 标题
* @param content 内容
* @param PicUrl 图片
* @param titleUrl title链接
*/
public static void shareQzone(String title, String content, String PicUrl, String titleUrl, PlatformActionListener mPlatformActionListener) {
QZone.ShareParams sp = new QZone.ShareParams();
sp.setTitle(title);
sp.setText(content);
sp.setTitleUrl(titleUrl); // 标题的超链接
if (PicUrl != null) {
sp.setImageUrl(PicUrl);
}
Platform qzone = PublicStaticData.myShareSDK.getPlatform(QZone.NAME);
qzone.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
qzone.share(sp);
}
/**
* QQ
*
* @param title
* @param content
* @param PicUrl
* @param titleUrl
*/
public static void shareQQ(String title, String content, String PicUrl, String titleUrl, PlatformActionListener mPlatformActionListener) {
QQ.ShareParams sp = new QQ.ShareParams();
sp.setTitle(title);
sp.setText(content);
sp.setTitleUrl(titleUrl); // 标题的超链接
if (PicUrl != null) {
sp.setImageUrl(PicUrl);
}
Platform qq = PublicStaticData.myShareSDK.getPlatform(QQ.NAME);
qq.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
qq.share(sp);
}
/**
* 收藏
*
* @param title
* @param content
* @param PicUrl
* @param titleUrl
*/
public static void shareWXF(String title, String content, String PicUrl, String titleUrl, PlatformActionListener mPlatformActionListener) {
WechatFavorite.ShareParams sp = new WechatFavorite.ShareParams();
sp.setTitle(title);
sp.setText(content);
sp.setTitleUrl(titleUrl); // 标题的超链接
if (PicUrl != null) {
sp.setImageUrl(PicUrl);
}
sp.setShareType(Platform.SHARE_IMAGE);
sp.setUrl("http://www.163.com/");
Platform wxf = PublicStaticData.myShareSDK.getPlatform(WechatFavorite.NAME);
wxf.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
wxf.share(sp);
}
/**
* 朋友圈
*
* @param title
* @param content
* @param PicUrl
* @param titleUrl
*/
public static void shareWXM(String title, String content, String PicUrl, String titleUrl, PlatformActionListener mPlatformActionListener) {
WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
sp.setTitle(title);
sp.setText(content);
sp.setTitleUrl(titleUrl); // 标题的超链接
if (PicUrl != null) {
sp.setImageUrl(PicUrl);
}
sp.setShareType(Platform.SHARE_IMAGE);
sp.setUrl("http://www.sina.com.cn");
Platform wxm = PublicStaticData.myShareSDK.getPlatform(WechatMoments.NAME);
wxm.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
wxm.share(sp);
}
/**
* 微信
*
* @param title
* @param content
* @param PicUrl
* @param titleUrl Platform.SHARE_TEXT(分享文本),
* Platform.SHARE_IMAGE(分享图片),
* Platform.SHARE_WEBPAGE(分享网页,既图文分享),
* Platform.SHARE_MUSIC(分享音频),
* Platform.SHARE_VIDEO(分享视频),
* Platform.SHARE_APPS(分享应用,仅微信支持),
* Platform.SHARE_FILE(分享文件,仅微信支持)
* Platform.SHARE_EMOJI(分享表情,仅微信支持)
*/
public static void shareWX(String title, String content, String PicUrl, String titleUrl, PlatformActionListener mPlatformActionListener) {
Wechat.ShareParams sp = new Wechat.ShareParams();
sp.setTitle(title);
sp.setText(content);
sp.setTitleUrl(titleUrl); // 标题的超链接
if (PicUrl != null) {
sp.setImageUrl(PicUrl);
}
sp.setShareType(Platform.SHARE_IMAGE);
sp.setUrl("http://qq.com");
Platform wx = PublicStaticData.myShareSDK.getPlatform(Wechat.NAME);
wx.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
// 执行图文分享
wx.share(sp);
}
/**
* 登录
*/
public static void Login(String name, PlatformActionListener mPlatformActionListener) {
Platform mPlatform = ShareSDK.getPlatform(name);
mPlatform.setPlatformActionListener(mPlatformActionListener);
mPlatform.authorize();//单独授权,OnComplete返回的hashmap是空的
mPlatform.showUser(null);//授权并获取用户信息
}
}
在Activity中的使用
String picurl="http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg";
case R.id.openShare:
showShare("sharesdk测试", "sharesdk测试", "http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
break;
case R.id.QQLogin://QQ登录
SharesdkType = ShareSDKUtils.LOGINTYPE;
ShareSDKUtils.Login(QQ.NAME, this);
break;
case R.id.WXLogin://微信登录
SharesdkType = ShareSDKUtils.LOGINTYPE;
ShareSDKUtils.Login(Wechat.NAME, this);
break;
case R.id.SinaLogin://微博登录
SharesdkType = ShareSDKUtils.LOGINTYPE;
ShareSDKUtils.Login(SinaWeibo.NAME, this);
break;
case R.id.WXLike://收藏分享(微信需要有图片)
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareWXF("微信收藏分享测试标题sharesdk", "微信收藏分享测试内容sharesdk", picurl, "http://www.haidu.com", this);
break;
case R.id.WXfriendsshare://朋友圈分享(微信需要有图片)
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareWXM("朋友圈分享测试标题sharesdk", "朋友圈分享测试内容sharesdk", picurl, "http://www.haidu.com", this);
break;
case R.id.WXshare://微信分享(微信需要有图片)
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareWX("微信分享测试标题sharesdk", "微信分享测试内容sharesdk", picurl, "http://www.haidu.com", this);
break;
case R.id.Sinashare://微博分享
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareSina("Sina分享测试sharesdk", null, this);
break;
case R.id.QZONEshare://空间
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareQzone("Qzone分享测试标题sharesdk", "Qzone分享测试内容sharesdk", picurl, "http://www.haidu.com", this);
break;
case R.id.QQshare://QQ
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareQQ("Qzone分享测试标题sharesdk", "Qzone分享测试内容sharesdk", picurl, "http://www.haidu.com", this);
break;
二、使用友盟来集成(友盟版本v6.0.0(精简版)):
1、此处省略注册、下载sdk、准备key的过程,直接从集成开始。
2、权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
3、添加Activity信息(此处使用微博精简版,新浪微博不需要回调Activity)此处注意tencent后面的appid要保持和您配置的QQ的appid一致:
<!-- ###################添加UmengAppkey###################### -->
<meta-data
android:name="UMENG_APPKEY"
android:value="577c404367e58e6b1f001a40">
</meta-data>
<!-- 调用微信,需要注册的回调activity -->
<activity
android:name=".wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- 调用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" />
<data android:scheme="tencent100424468" />
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize"/>
4、上代码:
4.0初始化:
PlatformConfig.setWeixin("wx282305cc871b77bc", "bdf0b6e3ff283ec29a12abf40ff62c1a");
PlatformConfig.setSinaWeibo("170221996", "75314930606f946864ee0d7aeecc46ed");
Config.REDIRECT_URL = "http://sns.whalecloud.com/sina2/callback";//您新浪后台的回调地址
PlatformConfig.setQQZone("1105747496", "EVHTBI0P5NDpuNIZ");
UMShareAPI.get(this);
4.1打开分享面板
new ShareAction(MainActivity.this).
withText("测试啊测试啊") .
withTitle("测试啊测试啊") .
withMedia(new UMImage(MainActivity.this, picurl))
.setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.WEIXIN_FAVORITE)
.setCallback(umShareListener).open();
4.2分享到指定平台
/**
* 分享
*
* @param mActivity
* @param title 标题
* @param context 内容
* @param Sharetype 分享方式
* SHARE_MEDIA.WEIXIN_FAVORITE
* SHARE_MEDIA.WEIXIN_CIRCLE
* SHARE_MEDIA.WEIXIN
* SHARE_MEDIA.SINA
* SHARE_MEDIA.QZONE
* SHARE_MEDIA.QQ
* @param picurl 图片地址
*/
public static void SharePic(Activity mActivity, String title, String context, SHARE_MEDIA Sharetype, String picurl, UMShareListener umShareListener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(mActivity, mPermissionList, 123);
}
if (picurl != null) {
new ShareAction(mActivity).setPlatform(Sharetype)
.withText(context)
.withTitle(title)
.withMedia(new UMImage(mActivity, picurl))
.setCallback(umShareListener)
.share();
} else {
new ShareAction(mActivity).setPlatform(Sharetype)
.withText(context)
.withTitle(title)
.setCallback(umShareListener)
.share();
}
}
4.3分享监听回调
/**
* 分享监听
*/
public static UMShareListener umShareListener = new UMShareListener() {
@Override
public void onResult(SHARE_MEDIA platform) {
Log.d("plat","platform"+platform);
Toast.makeText(myActivity, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(myActivity,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
if(t!=null){
Log.d("throw","throw:"+t.getMessage());
}
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(myActivity,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
}
};
4.4 登录
/**
* @param mActivity
* @param type 登录方式
* SHARE_MEDIA.QQ
* SHARE_MEDIA.WEIXIN
* SHARE_MEDIA.SINA
*/
public static void UmengLogin(Activity mActivity, SHARE_MEDIA type, UMAuthListener umAuthListener) {
PublicStaticData.mShareAPI = UMShareAPI.get(mActivity);
PublicStaticData.mShareAPI.doOauthVerify(mActivity, type, umAuthListener);
}
4.5 登录回调
/**
* 第三方登录监听
*/
public static UMAuthListener umAuthListener = new UMAuthListener() {
@Override
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
Toast.makeText(myActivity, "授权成功", Toast.LENGTH_SHORT).show();
Log.e("data",data.toString());
}
@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText( myActivity, "授权失败", Toast.LENGTH_SHORT).show();
Log.e("data",t.toString()+"");
}
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText( myActivity, "关闭授权", Toast.LENGTH_SHORT).show();
Log.e("data","关闭授权");
}
};
5.在一些低端手机上可能会出现QQ没有回调的情况,需要在Activity中添加如下代码:
/**
* 友盟QQ登录需要的回调 在有些低端手机上登录之后不走回调,需要这个方法
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).HandleQQError(MainActivity.this,requestCode,umAuthListener);
PublicStaticData.mShareAPI.onActivityResult(requestCode, resultCode, data);
}
6、同样是没有回调,这次是微信,需要在WXEntryActivity中加入代码:
@Override
protected void handleIntent(Intent intent){
mWxHandler.setAuthListener(new UMAuthListener() {
@Override
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
Log.e("UMWXHandler fsdfsdfs"+data.toString());
}
@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
Log.e("UMWXHandler fsdfsdfs",t.toString());
}
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Log.e("UMWXHandler fsdfsdfs","onCancel");
}
} );
super.handleIntent(intent);
}
7、至此使用友盟集成常见分三种登录分享方式就完成了。上整体代码UmengShare.java:
package com.example.lql.sharesdk.utils;
import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
/**
* 作者:dell or Xiaomi Li
* 时间: 2018/1/25
* 内容:把回调放在activity里边,就能直接拿到结果过处理了
* 最后修改:
*/
public class UmengShare {
/**
* 分享
*
* @param mActivity
* @param title 标题
* @param context 内容
* @param Sharetype 分享方式
* SHARE_MEDIA.WEIXIN_FAVORITE
* SHARE_MEDIA.WEIXIN_CIRCLE
* SHARE_MEDIA.WEIXIN
* SHARE_MEDIA.SINA
* SHARE_MEDIA.QZONE
* SHARE_MEDIA.QQ
* @param picurl 图片地址
*/
public static void SharePic(Activity mActivity, String title, String context, SHARE_MEDIA Sharetype, String picurl, UMShareListener umShareListener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(mActivity, mPermissionList, 123);
}
if (picurl != null) {
new ShareAction(mActivity).setPlatform(Sharetype)
.withText(context)
.withTitle(title)
.withMedia(new UMImage(mActivity, picurl))
.setCallback(umShareListener)
.share();
} else {
new ShareAction(mActivity).setPlatform(Sharetype)
.withText(context)
.withTitle(title)
.setCallback(umShareListener)
.share();
}
}
/**
* @param mActivity
* @param type 登录方式
* SHARE_MEDIA.QQ
* SHARE_MEDIA.WEIXIN
* SHARE_MEDIA.SINA
*/
public static void UmengLogin(Activity mActivity, SHARE_MEDIA type, UMAuthListener umAuthListener) {
PublicStaticData.mShareAPI = UMShareAPI.get(mActivity);
PublicStaticData.mShareAPI.doOauthVerify(mActivity, type, umAuthListener);
}
}
在Activity中使用:
case R.id.openShareUmeng:
new ShareAction(MainActivity.this).withText("测试啊测试啊")
.withTitle("测试啊测试啊")
.withMedia(new UMImage(MainActivity.this, picurl))
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE)
.setCallback(this).open();
break;
case R.id.QQLoginUmeng://QQ登录
UmengShare.UmengLogin(MainActivity.this, SHARE_MEDIA.QQ, this);
break;
case R.id.WXLoginUmeng://微信登录
UmengShare.UmengLogin(MainActivity.this, SHARE_MEDIA.WEIXIN, this);
break;
case R.id.SinaLoginUmeng://微博登录
UmengShare.UmengLogin(MainActivity.this, SHARE_MEDIA.SINA, this);
break;
case R.id.WXLikeUmeng://收藏分享(微信需要有图片)
UmengShare.SharePic(MainActivity.this, "测试收藏", "测试收藏内容", SHARE_MEDIA.WEIXIN_FAVORITE, picurl, this);
break;
case R.id.WXfriendsshareUmeng://朋友圈分享(微信需要有图片)
UmengShare.SharePic(MainActivity.this, "测试朋友圈", "测试朋友圈内容", SHARE_MEDIA.WEIXIN_CIRCLE, picurl, this);
break;
case R.id.WXshareUmeng://微信分享(微信需要有图片)
UmengShare.SharePic(MainActivity.this, "测试分享", "测试分享内容", SHARE_MEDIA.WEIXIN, picurl, this);
break;
case R.id.SinashareUmeng://微博分享
UmengShare.SharePic(MainActivity.this, "测试微博", "测试微博内容", SHARE_MEDIA.SINA, null, this);
break;
case R.id.QZONEshareUmeng://空间
UmengShare.SharePic(MainActivity.this, "测试空间", "测试空间内容", SHARE_MEDIA.QZONE, null, this);
break;
case R.id.QQshareUmeng://QQ
UmengShare.SharePic(MainActivity.this, "测试QQ", "测试QQ内容", SHARE_MEDIA.QQ, null, this);
break;
三、Activity完整代码:
package com.example.lql.sharesdk;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.lql.sharesdk.utils.LogUtils;
import com.example.lql.sharesdk.utils.PublicStaticData;
import com.example.lql.sharesdk.utils.ShareSDKUtils;
import com.example.lql.sharesdk.utils.UmengShare;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import java.util.HashMap;
import java.util.Map;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
public class MainActivity extends Activity implements View.OnClickListener, PlatformActionListener, UMShareListener, UMAuthListener {
private android.widget.Button openShare;
private Button QQLogin;
private Button WXLogin;
private Button SinaLogin;
private Button QQshare;
private Button QZONEshare;
private Button Sinashare;
private Button WXshare;
private Button WXfriendsshare;
private Button WXLike;
private Button openShareUmeng;
private Button QQLoginUmeng;
private Button WXLoginUmeng;
private Button SinaLoginUmeng;
private Button QQshareUmeng;
private Button QZONEshareUmeng;
private Button SinashareUmeng;
private Button WXshareUmeng;
private Button WXfriendsshareUmeng;
private Button WXLikeUmeng;
String picurl = "http://pic33.nipic.com/20130916/3420027_192919547000_2.jpg";//测试图片地址
String titleurl = "http://www.haidu.com";//titleurl
private int SharesdkType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
showShare("测试标题", "测试文本", picurl,titleurl);
}
private void initView() {
this.SinaLogin = (Button) findViewById(R.id.SinaLogin);
this.WXLogin = (Button) findViewById(R.id.WXLogin);
this.QQLogin = (Button) findViewById(R.id.QQLogin);
this.openShare = (Button) findViewById(R.id.openShare);
this.WXLike = (Button) findViewById(R.id.WXLike);
this.WXfriendsshare = (Button) findViewById(R.id.WXfriendsshare);
this.WXshare = (Button) findViewById(R.id.WXshare);
this.Sinashare = (Button) findViewById(R.id.Sinashare);
this.QZONEshare = (Button) findViewById(R.id.QZONEshare);
this.QQshare = (Button) findViewById(R.id.QQshare);
this.WXLikeUmeng = (Button) findViewById(R.id.WXLikeUmeng);
this.WXfriendsshareUmeng = (Button) findViewById(R.id.WXfriendsshareUmeng);
this.WXshareUmeng = (Button) findViewById(R.id.WXshareUmeng);
this.SinashareUmeng = (Button) findViewById(R.id.SinashareUmeng);
this.QZONEshareUmeng = (Button) findViewById(R.id.QZONEshareUmeng);
this.QQshareUmeng = (Button) findViewById(R.id.QQshareUmeng);
this.SinaLoginUmeng = (Button) findViewById(R.id.SinaLoginUmeng);
this.WXLoginUmeng = (Button) findViewById(R.id.WXLoginUmeng);
this.QQLoginUmeng = (Button) findViewById(R.id.QQLoginUmeng);
this.openShareUmeng = (Button) findViewById(R.id.openShareUmeng);
WXLikeUmeng.setOnClickListener(this);
WXfriendsshareUmeng.setOnClickListener(this);
WXshareUmeng.setOnClickListener(this);
SinashareUmeng.setOnClickListener(this);
QZONEshareUmeng.setOnClickListener(this);
QQshareUmeng.setOnClickListener(this);
SinaLoginUmeng.setOnClickListener(this);
WXLoginUmeng.setOnClickListener(this);
QQLoginUmeng.setOnClickListener(this);
openShareUmeng.setOnClickListener(this);
this.WXLike.setOnClickListener(this);
this.WXfriendsshare.setOnClickListener(this);
this.WXshare.setOnClickListener(this);
this.Sinashare.setOnClickListener(this);
this.QZONEshare.setOnClickListener(this);
this.QQshare.setOnClickListener(this);
this.openShare.setOnClickListener(this);
this.SinaLogin.setOnClickListener(this);
this.WXLogin.setOnClickListener(this);
this.QQLogin.setOnClickListener(this);
}
/**
* @param title 标题
* @param text 内容
* @param picurl 图片链接
* <p>
* QQ和QQ空间设置分享链接使用setTitleUrl();
* 设置标题:setTitle
* 设置内容:setText
* 设置网络图片:oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
* 设置本地图片: //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
* <p>
* 微信
* url仅在微信(包括好友和朋友圈)中使用
* oks.setUrl("http://qq.com");
*/
private void showShare(String title, String text, String picurl, String titleUrl) {
OnekeyShare oks = new OnekeyShare();
//关闭sso授权
oks.disableSSOWhenAuthorize();
oks.setTitleUrl(titleUrl);
oks.setTitle(title);
oks.setText(text);
if (picurl != null) {
oks.setImageUrl(picurl);
}
// 启动分享GUI
oks.show(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.openShare:
showShare("sharesdk测试", "sharesdk测试", "http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg",titleurl);
break;
case R.id.QQLogin://QQ登录
SharesdkType = ShareSDKUtils.LOGINTYPE;
ShareSDKUtils.Login(QQ.NAME, this);
break;
case R.id.WXLogin://微信登录
SharesdkType = ShareSDKUtils.LOGINTYPE;
ShareSDKUtils.Login(Wechat.NAME, this);
break;
case R.id.SinaLogin://微博登录
SharesdkType = ShareSDKUtils.LOGINTYPE;
ShareSDKUtils.Login(SinaWeibo.NAME, this);
break;
case R.id.WXLike://收藏分享(微信需要有图片)
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareWXF("微信收藏分享测试标题sharesdk", "微信收藏分享测试内容sharesdk", picurl, titleurl, this);
break;
case R.id.WXfriendsshare://朋友圈分享(微信需要有图片)
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareWXM("朋友圈分享测试标题sharesdk", "朋友圈分享测试内容sharesdk", picurl, titleurl, this);
break;
case R.id.WXshare://微信分享(微信需要有图片)
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareWX("微信分享测试标题sharesdk", "微信分享测试内容sharesdk", picurl, titleurl, this);
break;
case R.id.Sinashare://微博分享
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareSina("Sina分享测试sharesdk", null, this);
break;
case R.id.QZONEshare://空间
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareQzone("Qzone分享测试标题sharesdk", "Qzone分享测试内容sharesdk", picurl, titleurl, this);
break;
case R.id.QQshare://QQ
SharesdkType = ShareSDKUtils.SHARETYPE;
ShareSDKUtils.shareQQ("Qzone分享测试标题sharesdk", "Qzone分享测试内容sharesdk", picurl, titleurl, this);
break;
//--------------------------------------------------------------以上是Sharesdk---------------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++华丽分分割线++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//----------------------------------------------------------------以下是友盟-------------------------------------------------------------
case R.id.openShareUmeng:
new ShareAction(MainActivity.this).withText("测试啊测试啊")
.withTitle("测试啊测试啊")
.withMedia(new UMImage(MainActivity.this, picurl))
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE)
.setCallback(this).open();
break;
case R.id.QQLoginUmeng://QQ登录
UmengShare.UmengLogin(MainActivity.this, SHARE_MEDIA.QQ, this);
break;
case R.id.WXLoginUmeng://微信登录
UmengShare.UmengLogin(MainActivity.this, SHARE_MEDIA.WEIXIN, this);
break;
case R.id.SinaLoginUmeng://微博登录
UmengShare.UmengLogin(MainActivity.this, SHARE_MEDIA.SINA, this);
break;
case R.id.WXLikeUmeng://收藏分享(微信需要有图片)
UmengShare.SharePic(MainActivity.this, "测试收藏", "测试收藏内容", SHARE_MEDIA.WEIXIN_FAVORITE, picurl, this);
break;
case R.id.WXfriendsshareUmeng://朋友圈分享(微信需要有图片)
UmengShare.SharePic(MainActivity.this, "测试朋友圈", "测试朋友圈内容", SHARE_MEDIA.WEIXIN_CIRCLE, picurl, this);
break;
case R.id.WXshareUmeng://微信分享(微信需要有图片)
UmengShare.SharePic(MainActivity.this, "测试分享", "测试分享内容", SHARE_MEDIA.WEIXIN, picurl, this);
break;
case R.id.SinashareUmeng://微博分享
UmengShare.SharePic(MainActivity.this, "测试微博", "测试微博内容", SHARE_MEDIA.SINA, null, this);
break;
case R.id.QZONEshareUmeng://空间
UmengShare.SharePic(MainActivity.this, "测试空间", "测试空间内容", SHARE_MEDIA.QZONE, null, this);
break;
case R.id.QQshareUmeng://QQ
UmengShare.SharePic(MainActivity.this, "测试QQ", "测试QQ内容", SHARE_MEDIA.QQ, null, this);
break;
}
}
/**
* 友盟QQ登录需要的回调 在有些低端手机上登录之后不走回调,需要这个方法
*
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).HandleQQError(MainActivity.this, requestCode, this);
PublicStaticData.mShareAPI.onActivityResult(requestCode, resultCode, data);
}
//=======================================================ShareSDK的回调=======================================================
/**
* 这里是ShareSDK的回调
*
* @param platform
* @param i
* @param hashMap
*/
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
if (SharesdkType == ShareSDKUtils.LOGINTYPE) {
LogUtils.Loge("登录成功");
LogUtils.Loge("openid:" + platform.getDb().getUserId());//拿到登录后的openid
LogUtils.Loge("username:" + platform.getDb().getUserName());//拿到登录后的昵称
} else {
LogUtils.Loge("分享成功");
}
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
LogUtils.Loge(SharesdkType == ShareSDKUtils.LOGINTYPE ? "登录失败" + throwable.toString() : "分享失败" + throwable.toString());
}
@Override
public void onCancel(Platform platform, int i) {
LogUtils.Loge(SharesdkType == ShareSDKUtils.LOGINTYPE ? "登录取消" : "分享取消");
}
//=======================================================友盟的回调=======================================================
//这里注意:友盟的分享和登陆回调是分开的
/**
* 友盟分享的回调
*
* @param share_media
*/
@Override
public void onResult(SHARE_MEDIA share_media) {
LogUtils.Loge("分享成功啦");
}
@Override
public void onError(SHARE_MEDIA share_media, Throwable throwable) {
LogUtils.Loge("分享失败啦" + throwable.toString());
}
@Override
public void onCancel(SHARE_MEDIA share_media) {
LogUtils.Loge("分享取消了");
}
/**
* 友盟登陆的回调
*
* @param share_media
*/
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
LogUtils.Loge("授权成功");
LogUtils.Loge("data:" + map.toString());
LogUtils.Loge("openid:" + map.get("openid"));
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
LogUtils.Loge("授权失败:" + throwable.toString());
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
LogUtils.Loge("关闭授权");
}
}
最后,两种方式都集成好了,小伙伴们赶紧去使用吧。
github:https://github.com/LiQinglin007/Sharesdk