友盟分享

1,添加依赖

//友盟分享
compile files('libs/libammsdk.jar')
compile files('libs/open_sdk_r5778_lite.jar')
compile files('libs/SocialSDK_QQ_Full.jar')
compile files('libs/SocialSDK_WeiXin_Full.jar')
compile files('libs/umeng_shareboard_widget.jar')
compile files('libs/umeng_social_api.jar')
compile files('libs/umeng_social_net.jar')
compile files('libs/umeng_social_shareboard.jar')
compile files('libs/umeng_social_tool.jar'),
2,添加资源

友盟分享

友盟分享

友盟分享

3,把封装的类考进来

ShareUtil

4,初始化

private void initShareInfo() {
    Config.DEBUG = true;
    UMShareAPI.get(this);
    //微信 wx4d5fa990e9695e89, 51c6b225fa101486f6829bf55af46970
    PlatformConfig.setWeixin("wx4d5fa990e9695e89", "51c6b225fa101486f6829bf55af46970");
    //QQ 1106623962, z5VcAfXdjOQ7T25y
    PlatformConfig.setQQZone("1106623962", "z5VcAfXdjOQ7T25y");
}

5,使用

//分享
private void shareInit() {
    ShareUtil shareUtil = new ShareUtil(this);

    ShareContent shareInfo = new ShareContent();
    //标题
    shareInfo.mTitle = "车务代办,就上车务匠";
    //图片
    shareInfo.mMedia = new UMImage(this, R.drawable.ic_logo);
    //描述(描述必须小于20)
    shareInfo.mText = "别等被骗了,才想到用车务代办!车务匠APP全国车务人都在用的代办平台,赶快来下载吧~~~";

    shareInfo.mTargetUrl = mApplication.getUserShareURL();
    if (mApplication.getUserShareURL() != null) {
        if (!mApplication.getUserShareURL().contains("recommendId")) {
            shareInfo.mTargetUrl = mApplication.getUserShareURL() + "?recommendId=" + mApplication.getUserInfo().getUserId();// String类型;
        }
    }

    shareUtil.setParam(shareInfo);
    shareUtil.showChoose();

6,mainfest

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

<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="tencent1106623962" />//换成自己的
    </intent-filter>
</activity>
<activity
    android:name="com.tencent.connect.common.AssistActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

最后贴出

public class ShareUtil {
    Context context;
    Activity activity;

    private ShareAction shareAction;

    private ShareContent shareInfo;

    private UMShareListener umSetShareListener;

    final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]{SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ};
//    SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE

    public ShareUtil(Activity activity) {
        this.activity = activity;
        context = activity;
    }

    /**
     * 添加分享的内容
     */
    public void setParam(ShareContent shareInfo) {
        this.shareInfo = shareInfo;
    }

    /**
     * 分享弹窗框
     */
    public void showChoose() {
        if (umSetShareListener == null || "".equals(umSetShareListener)) {
            shareAction = new ShareAction(activity)
                    .setDisplayList(displaylist)
                    .setContentList(shareInfo, shareInfo, shareInfo)
                    .setCallback(umShareListener);
            shareAction.open();
        } else {
            shareAction = new ShareAction(activity)
                    .setDisplayList(displaylist)
                    .setContentList(shareInfo, shareInfo, shareInfo)
                    .setCallback(umSetShareListener);
            shareAction.open();
        }

    }

    /**
     * 通过分享
     *
     * @param share_media 单个分享
     */
    public void shareByType(SHARE_MEDIA share_media) {

        shareAction = new ShareAction(activity)
                .setPlatform(share_media)//单个平台传入平台
                .withText(shareInfo.mText)//分享内容
                .withTitle(shareInfo.mTitle)
                .withMedia(new UMImage(activity, R.drawable.ic_logo))
                .withTargetUrl(shareInfo.mTargetUrl)
                .setCallback(umShareListener);

        shareAction.share();
    }

    /**
     * /**
     * 分享结果监听
     */
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            shareAction.close();
            Toast.makeText(activity, " 分享成功啦", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            shareAction.close();
            Toast.makeText(activity, " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            shareAction.close();
            Toast.makeText(activity, " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    public UMShareListener getUmSetShareListener() {
        return umSetShareListener;
    }

    public void setUmSetShareListener(UMShareListener umSetShareListener) {
        this.umSetShareListener = umSetShareListener;
    }

    public ShareAction getShareAction() {
        return shareAction;
    }

    public void setShareAction(ShareAction shareAction) {
        this.shareAction = shareAction;
    }
}