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”放进去,注意,这个类不需要动,不需要手动调用!!
![WXEntryActivity的位置](http://img.blog.csdn.net/20161015153103561)
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 android.content.Context; import android.util.Log; import java.util.HashMap; 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; /** * Created by LQL on 2016/10/14. */ public class ShareSDKUtils { private static String type=""; /** * 分享到微博 * @param text 文本内容 * @param picUrl 网络图片 (通过审核后才能添加) */ public static void shareSina(String text,String picUrl,Context context){ 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); } /** * 分享到QQ空间 * @param title 标题 * @param content 内容 * @param PicUrl 图片 * @param titleUrl title链接 */ public static void shareQzone(String title,String content,String PicUrl,String titleUrl){ type="share"; QZone.ShareParams sp = new QZone.ShareParams(); sp.setTitle(title); sp.setText(content); if(titleUrl!=null){ 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){ type="share"; QQ.ShareParams sp=new QQ.ShareParams(); sp.setTitle(title); sp.setText(content); if(titleUrl!=null){ 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){ type="share"; WechatFavorite.ShareParams sp=new WechatFavorite.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://www.163.com/"); Platform qq = PublicStaticData.myShareSDK.getPlatform (WechatFavorite.NAME); qq. setPlatformActionListener (mPlatformActionListener); // 设置分享事件回调 // 执行图文分享 qq.share(sp); } /** * 朋友圈 * @param title * @param content * @param PicUrl * @param titleUrl */ public static void shareWXM(String title,String content,String PicUrl,String titleUrl){ type="share"; WechatMoments.ShareParams sp=new WechatMoments.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://www.sina.com.cn"); Platform qq = PublicStaticData.myShareSDK.getPlatform (WechatMoments.NAME); qq. setPlatformActionListener (mPlatformActionListener); // 设置分享事件回调 // 执行图文分享 qq.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){ 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); } /** * 登录 */ public static void Login(String name){ type="login"; Platform mPlatform = ShareSDK.getPlatform(name); mPlatform.setPlatformActionListener(mPlatformActionListener); mPlatform.authorize();//单独授权,OnComplete返回的hashmap是空的 mPlatform.showUser(null);//授权并获取用户信息 } 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","分享取消"); } } }; }
在Activity中的使用
String picurl="http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg"; case R.id.openShare://打开分享面板,直接调用上边写到的方法 showShare("sharesdk测试","sharesdk测试",picurl); break; case R.id.QQLogin://QQ登录 ShareSDKUtils.Login(QQ.NAME); break; case R.id.WXLogin://微信登录 ShareSDKUtils.Login(Wechat.NAME); break; case R.id.SinaLogin://微博登录 ShareSDKUtils.Login(SinaWeibo.NAME); break; case R.id.WXLike://收藏分享(微信需要有图片) ShareSDKUtils.shareWXF("微信收藏分享测试标题sharesdk","微信收藏分享测试内容sharesdk",picurl,null); break; case R.id.WXfriendsshare://朋友圈分享(微信需要有图片) ShareSDKUtils.shareWXM("朋友圈分享测试标题sharesdk","朋友圈分享测试内容sharesdk",picurl,null); break; case R.id.WXshare://微信分享(微信需要有图片) ShareSDKUtils.shareWX("微信分享测试标题sharesdk","微信分享测试内容sharesdk",picurl,null); break; case R.id.Sinashare://微博分享 ShareSDKUtils.shareSina("Sina分享测试sharesdk",null,MainActivity.this); break; case R.id.QZONEshare://空间 ShareSDKUtils.shareQzone("Qzone分享测试标题sharesdk","Qzone分享测试内容sharesdk",picurl,null); break; case R.id.QQshare://QQ ShareSDKUtils.shareQQ("Qzone分享测试标题sharesdk","Qzone分享测试内容sharesdk",picurl,null); 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){ UmengShare.myActivity=mActivity; 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){ UmengShare.myActivity=mActivity; 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 android.util.Log; import android.widget.Toast; 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.Map; /** * Created by LQL on 2016/10/15. */ public class UmengShare { private static Activity myActivity; /** * 分享 * @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){ UmengShare.myActivity=mActivity; 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){ UmengShare.myActivity=mActivity; PublicStaticData.mShareAPI = UMShareAPI.get( mActivity ); PublicStaticData.mShareAPI.doOauthVerify(mActivity,type, umAuthListener); } /** * 第三方登录监听 */ 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","关闭授权"); } }; /** * 分享监听 */ 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(); } }; }
在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(umShareListener).open(); break; case R.id.QQLoginUmeng://QQ登录 UmengShare.UmengLogin(MainActivity.this,SHARE_MEDIA.QQ); break; case R.id.WXLoginUmeng://微信登录 UmengShare.UmengLogin(MainActivity.this,SHARE_MEDIA.WEIXIN); break; case R.id.SinaLoginUmeng://微博登录 UmengShare.UmengLogin(MainActivity.this,SHARE_MEDIA.SINA); break; case R.id.WXLikeUmeng://收藏分享(微信需要有图片) UmengShare.SharePic(MainActivity.this,"测试收藏","测试收藏内容",SHARE_MEDIA.WEIXIN_FAVORITE,picurl); break; case R.id.WXfriendsshareUmeng://朋友圈分享(微信需要有图片) UmengShare.SharePic(MainActivity.this,"测试朋友圈","测试朋友圈内容",SHARE_MEDIA.WEIXIN_CIRCLE,picurl); break; case R.id.WXshareUmeng://微信分享(微信需要有图片) UmengShare.SharePic(MainActivity.this,"测试分享","测试分享内容",SHARE_MEDIA.WEIXIN,picurl); break; case R.id.SinashareUmeng://微博分享 UmengShare.SharePic(MainActivity.this,"测试微博","测试微博内容",SHARE_MEDIA.SINA,null); break; case R.id.QZONEshareUmeng://空间 UmengShare.SharePic(MainActivity.this,"测试空间","测试空间内容",SHARE_MEDIA.QZONE,null); break; case R.id.QQshareUmeng://QQ UmengShare.SharePic(MainActivity.this,"测试QQ","测试QQ内容",SHARE_MEDIA.QQ,null); break;
最后,两种方式都集成好了,小伙伴们赶紧去使用吧。本人能力有效,如果bug纯属正常!建议使用ShareSDK!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
android时间选择控件之TimePickerView使用方法详解
这篇文章主要为大家详细介绍了android时间选择控件之TimePickerView的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-09-09
最新评论