美文网首页Cocos Creator
cocos creator安卓接入默往分享

cocos creator安卓接入默往分享

作者: Augus_Einstein | 来源:发表于2020-01-16 18:24 被阅读0次

结合上一篇文档 cocos creator安卓接入默往登录

一、AppActivity.java里改动

//引分享包
import com.mostone.open.sdk.ShareAction;
import com.mostone.open.sdk.listener.MShareListener;
import com.mostone.open.sdk.media.MImageData;
import com.mostone.open.sdk.media.MWebData;

//静态变量声明
public class AppActivity extends Cocos2dxActivity{

    //…
    //默往
    private static AppActivity ptrApp;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //..
        ptrApp = AppActivity.this;
        //..
    }
    
    public static void MoWangShareImage(final String path) {
        Bitmap bmp = null;
        if(path.startsWith("http")){
            try{
                bmp = BitmapFactory.decodeStream(new URL(path).openStream());
            } catch(Exception e) {
                e.printStackTrace();
            }
        }else{
            bmp = BitmapFactory.decodeFile(path);
        }
        Log.d(LOG_TAG,"分享图片");
        //分享结果回调
        AppActivity.shareMWImage(bmp, new MShareListener() {
            @Override
            public void onResult(BeanMResp beanMResp) {
                Log.d(LOG_TAG,"come in the Image listener");      
                switch (beanMResp.resCode) {
                    case BeanMResp.ResCode.RES_SEND_OK: {
                        Toast.makeText(AppActivity.sContext, "发送成功", Toast.LENGTH_LONG).show();
                        break;
                    }
                    case BeanMResp.ResCode.RES_SENT_FAILED: {
                        Toast.makeText(AppActivity.sContext, "发送失败", Toast.LENGTH_LONG).show();
                        break;
                    }
                    case BeanMResp.ResCode.RES_USER_CANCEL: {
                        Toast.makeText(AppActivity.sContext, "用户取消发送", Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
        });
    }

    public static void MWShareUrl(final String szTitle,final String szDesrc,final String szRedirectUrl)
    {                 
        AppActivity.shareMWUrl(szTitle, szDesrc,szRedirectUrl, com.chuangsheng.btx.R.mipmap.ic_launcher, new MShareListener() {
                @Override
                public void onResult(BeanMResp beanMResp) {   
                    Log.d(LOG_TAG,"come in the URL listener");
                    switch (beanMResp.resCode) {
                        case BeanMResp.ResCode.RES_SEND_OK: {
                            Toast.makeText(AppActivity.sContext, "发送成功", Toast.LENGTH_LONG).show();
                            break;
                        }
                        case BeanMResp.ResCode.RES_SENT_FAILED: {
                            Toast.makeText(AppActivity.sContext, "发送失败", Toast.LENGTH_LONG).show();
                            break;
                        }
                        case BeanMResp.ResCode.RES_USER_CANCEL: {
                            Toast.makeText(AppActivity.sContext, "用户取消发送", Toast.LENGTH_LONG).show();
                            break;
                        }
                    }
                }
        });
    }

    ////share interface
    
    public static void shareMWImage(Object imageObj, final MShareListener mShareListener) {
        //分享图片
        // withImage 分享图片有以下几种选择方式
        // .MImageData("imageurl");//网络图片
        // .MImageData(file);//本地文件
        // .MImageData(R.drawable.xxx);//资源文件
        // .MImageData(bitmap);//bitmap文件
        // 推荐使用网络图片
        // 本地图片大小最好不要超过250k,超过250k将会被压缩图片

        MImageData image = null;
        if (imageObj instanceof String) {
            image = new MImageData(ptrApp, (String) imageObj);
        } else if (imageObj instanceof File) {
            image = new MImageData(ptrApp, (File) imageObj);
        } else if (imageObj instanceof Integer) {
            image = new MImageData(ptrApp, (int) imageObj);
        } else if (imageObj instanceof Bitmap) {
            image = new MImageData(ptrApp, (Bitmap) imageObj);
        } 
        
        new ShareAction(ptrApp)
                .withMedia(image)//分享标题
                .setCallBack(mShareListener)
                .share();
    }

    public static void shareMWUrl(String title, String description, String url, int thumbRes, final MShareListener mShareListener) {
    
        //分享卡片分三种类型
        //通过设置withWebType 区分分享模式
        //WebType.APP          分享跳转app
        //WebType.HTML5        分享网页
        //WebType.HTML5AndAPP  混合分享模式,由用户点击卡片选择App或者html
        MWebData mWebData = new MWebData();
        mWebData.title = title;
        mWebData.content = description;
        mWebData.thumbData = new MImageData(ptrApp, url);       
        
        //混合分享模式
        //注意混合模式需要将app跳转模式和网页模式的相关参数都设置
        mWebData.androidJumpParam = title;//android分享卡片点击返回参数
        mWebData.iOSJumpParam = title;//ios分享卡片点击返回参数
        mWebData.webLink = url;
        mWebData.webType = MWebData.WebType.HTML5AndAPP;//设置分享类型为混合模式  
         

        new ShareAction(ptrApp)
                .withMedia(mWebData)//分享标题
                .setCallBack(mShareListener)
                .share();
    }

二、ThirdParty.js里加入默往分享调用方法

//默往分享
function ThirdPartyMoWangShareMessage(ShareInfo) {
    console.log('MWShare ' + JSON.stringify(ShareInfo));
    if (cc.sys.isNative) {
        if (cc.sys.OS_IOS == cc.sys.os) {
            jsb.reflection.callStaticMethod('AppController', 'MWSharetitle:description:url:', ShareInfo.title, ShareInfo.describe, ShareInfo.link);
        } else {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'MWShareUrl',
                '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V',
                ShareInfo.title, ShareInfo.describe,
                ShareInfo.link);
        }
    }
}

//默往分享图片
function ThirdPartyMoWangShareImg(Path) {
    console.log('MWShareTex ' + Path);
    if (cc.sys.isNative) {
        if (cc.sys.OS_IOS == cc.sys.os) {
            jsb.reflection.callStaticMethod('AppController', 'MoWangShareImage:', Path);
        } else {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'MoWangShareImage',
                '(Ljava/lang/String;)V', Path);
        }
    }
}

三、对应分享界面js添加点击事件调用分享方法

————————END

相关文章

网友评论

    本文标题:cocos creator安卓接入默往分享

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