欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 第三方登录、分享(ShareSDK、友盟)

程序员文章站 2024-03-05 10:00:36
为下边的项目做准备,写一个第三方登录、分享的demo。分别使用sharesdk和友盟来实现。 先说一下我对两者的使用上的感觉,个人感觉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!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。