美文网首页
ShareSDK集成篇

ShareSDK集成篇

作者: 冉冉升起的小太阳 | 来源:发表于2017-07-10 17:51 被阅读712次

先看下整体分享后结果

Paste_Image.png

1.进入ShareSDK官网

**申请AppKey 和 AppSecret **

Paste_Image.png

配置参数

2.权限配置

   <!-- 社会化分享star-->
    <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.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.SYSTEM_ALERT_WINDOW" />
    <!--社会化分享end-->

SDKActivity

    <!-- ShareSDK的activity -->
        <activity
            android:name="com.mob.tools.MobUIShell"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            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>

            <!--集成line客户端登录授权,需要添如下格式的过滤器-->
            <intent-filter>
                <data android:scheme="line.1477692153" />
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

        <!--微信分享回调 -->
        <activity
            android:name=".wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait" />

        <!--易信分享回调 -->
        <activity
            android:name=".yxapi.YXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:screenOrientation="portrait" />

        <!-- 支付宝分享回调 -->
        <activity
            android:name=".apshare.ShareEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true"/>
        <!--钉钉分享回调-->
        <activity
            android:name=".ddshare.DDShareActivity"
            android:launchMode="singleInstance"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>

        <!-- 填写您从Mob开发者后台中得到的Appkey和AppSecret -->
        <meta-data android:name="Mob-AppKey" android:value="Appkey"/>
        <meta-data android:name="Mob-AppSecret" android:value="AppSecret"/>

3.导入相关JAR包和xml文件 Class 文件

包含微信 微信朋友圈 QQ空间 QQ

Paste_Image.png

每个平台申请KEY 需要在里面配置

Paste_Image.png
    <SinaWeibo
Id="1"
SortId="1" 
//SortId是此平台在分享列表中的位置,由开发者自行定义,可以是任何整型数字,数值越大越靠后
                AppKey="568898243"
                AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"
                RedirectUrl="http://www.mob.com" 
//  其中的AppKey、AppSecret和RedirectUrl是您在新浪微博上注册开发者信息和应用后得到的信息
                Enable="true" />
//  Enable字段表示此平台是否有效,布尔值,默认为true,如果Enable为false

微信配置

//新建包名 wxapi
/*
 * 官网地站:http://www.mob.com
 * 技术支持QQ: 4006852216
 * 官方微信:ShareSDK   (如果发布新版本的话,我们将会第一时间通过微信将版本更新内容推送给您。如果使用过程中有任何问题,也可以通过微信与我们取得联系,我们将会在24小时内给予回复)
 *
 * Copyright (c) 2013年 mob.com. All rights reserved.
 */

package alva.zhangxin.com.wxapi;

import android.content.Intent;
import android.widget.Toast;
import cn.sharesdk.wechat.utils.WXAppExtendObject;
import cn.sharesdk.wechat.utils.WXMediaMessage;
import cn.sharesdk.wechat.utils.WechatHandlerActivity;

/** 微信客户端回调activity示例 */
public class WXEntryActivity extends WechatHandlerActivity {

    /**
     * 处理微信发出的向第三方应用请求app message
     * <p>
     * 在微信客户端中的聊天页面有“添加工具”,可以将本应用的图标添加到其中
     * 此后点击图标,下面的代码会被执行。Demo仅仅只是打开自己而已,但你可
     * 做点其他的事情,包括根本不打开任何页面
     */
    public void onGetMessageFromWXReq(WXMediaMessage msg) {
        if (msg != null) {
            Intent iLaunchMyself = getPackageManager().getLaunchIntentForPackage(getPackageName());
            startActivity(iLaunchMyself);
        }
    }

    /**
     * 处理微信向第三方应用发起的消息
     * <p>
     * 此处用来接收从微信发送过来的消息,比方说本demo在wechatpage里面分享
     * 应用时可以不分享应用文件,而分享一段应用的自定义信息。接受方的微信
     * 客户端会通过这个方法,将这个信息发送回接收方手机上的本demo中,当作
     * 回调。
     * <p>
     * 本Demo只是将信息展示出来,但你可做点其他的事情,而不仅仅只是Toast
     */
    public void onShowMessageFromWXReq(WXMediaMessage msg) {
        if (msg != null && msg.mediaObject != null
                && (msg.mediaObject instanceof WXAppExtendObject)) {
            WXAppExtendObject obj = (WXAppExtendObject) msg.mediaObject;
            Toast.makeText(this, obj.extInfo, Toast.LENGTH_SHORT).show();
        }
    }

}

Paste_Image.png

ShareSDK 配置
ShareSDK下载地址

Paste_Image.png Paste_Image.png Paste_Image.png

把相关内容复制到对应位置

4.初始化

//application中初始化
MobSDK.init(Context context);

5.微信端口需要注册注意内容

Android studio 获取签名参考
微信开放平台
签名需要MD5大写变小写冒号去掉

例如:      
MD5: 31:40:60:B1:4A:B1:6F:7A:63:BC:FC:CD:64:cB:65:4a
md5: 314060b14ab16f7a63bcfccd64cb654a

6.ShaeSDK 自定义界面

ShareSDK 本身自带一种分享UI

Paste_Image.png
自定义界面 Paste_Image.png
界面丑的一逼 对付看吧 思路就是自定义一个Dialog 分享看下面代码
    private void showShareQQ() {

        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle("测试分享的标题");
        sp.setTitleUrl("http://sharesdk.cn"); // 标题的超链接
        sp.setText("测试分享的文本");
        sp.setImageUrl("http://www.someserver.com/测试图片网络地址.jpg");
        sp.setSite("发布分享的网站名称");
        sp.setSiteUrl("发布分享网站的地址");
    

        Platform qzone = ShareSDK.getPlatform(QQ.NAME); 
      
        // 设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)
        qzone.setPlatformActionListener(new PlatformActionListener() {
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调,arg:平台对象,arg1:表示当前的动作,arg2:异常信息
            }

            public void onComplete(Platform arg0, int arg1, HashMap arg2) {
                //分享成功的回调
            }

            public void onCancel(Platform arg0, int arg1) {
                //取消分享的回调
            }
        });
// 执行图文分享
        qzone.share(sp);

    }

Platform qzone = ShareSDK.getPlatform(QQ.NAME); 不同分享 设置不同就行了 其他都一样

      Platform qzone = ShareSDK.getPlatform(Wechat.NAME);
      Platform qzone = ShareSDK.getPlatform(WechatMoments.NAME);

相关文章

网友评论

      本文标题:ShareSDK集成篇

      本文链接:https://www.haomeiwen.com/subject/wkluhxtx.html