审核通过之后就会如上图所示,审核已通过。一般应用审核通过之后只会有微信分享和收藏的功能:
某些接口是要收费的,具体要看自己的需求了,再提交应用的时候有很重要的两点,第一就是包名,安卓程序唯一标识就是包名,也是安装在手机上的唯一标识,这样系统才会识别出来这是两种不同的应用。还有一个就是应用签名:把apk文件装在手机上用微信开放平台的签名检测工具,输入自己的包名就可以得出应用签名了,然后把这个签名提交到微信开放平台上。(这里有一点需要特别注意,到后面出现无法登陆微信的情况很有可能是本地签名和微信开放平台的签名不一致所导致的,导出自己的apk时,要注意需要用keystore文件来对apk进行签名,这样带有keystore文件的apk以后就可以用这个keystore来签名了,尽量不要用deBUG.keystore。在以后微信开放平台上面的应用签名也可以修改)
在eclipse里面打开这个窗口进行keystore的导入(首先要自己创建一个keystore文件):
选择keystore文件之后点击apply,然后ok就可以了。
2.编写本地代码 由于我的项目是Cocos项目,要想调用Java层的微信SDK接口必须在本地编写逻辑代码,然后借助Jni来调用Java层的代码:
self.button_login:addtouchEventListener(function ( sender,eventType ) if eventType == ccui.touchEventType.ended then print('==================登录 LoginScene:btnEvent()') self:lodingAnimation() game.anysdk:login_native() end end)
然后走到anysdk:login_native里面(也可以直接走到Jni里面)
function AnySdk:login_native() local function LoginServer() local function callBack() MissionManager.getMission('login_mission','main_mission'):sendData(MDM_GR_logoN,SUB_GP_logoN_ACCOUNTS) end MissionManager.connect(LOGIN_SERVER_name,LOGIN_SERVER_IP,LOGIN_SERVER_PORT,callBack) end if device.platform == "androID" or device.platform == "ios" then local access_token = cc.UserDefault:getInstance():getStringForKey("access_token","") local openID = cc.UserDefault:getInstance():getStringForKey("openID","") if access_token ~= "" and openID ~= "" then self:getWeixinInfo(access_token,openID,LoginServer) else JniFun:create():longinWX(APP_ID,AppSecret) end else --self:getWeixinInfo("","",LoginServer) local function callBack() MissionManager.getMission('login_mission',SUB_GP_REGISTER_ACCOUNTS) end MissionManager.connect(LOGIN_SERVER_name,callBack) endend
然后进入到JniFun:loginWX里面:
#include "JniFun.h"#include "cocos2d.h"#include "ccluaEngine.h"#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID#include <Jni.h>#include "platform/androID/jni/JniHelper.h"#include "jubaosdk/ShellAPIJni.h"#endif#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS#include "IosHelper.h"#endif#define JAVA_CLASSname "com/wx/Native"using namespace cocos2d;
voID JniFun::longinWX(const char* APP_ID,const char* AppSecret) {#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticmethodInfo(minfo,JAVA_CLASSname,"LoginWX","(Ljava/lang/String;Ljava/lang/String;)V"); if (isHave) { Jstring jAPP_ID = minfo.env->NewStringUTF(APP_ID); Jstring jAppSecret = minfo.env->NewStringUTF(AppSecret); minfo.env->CallStaticVoIDMethod(minfo.classID,minfo.methodID,jAPP_ID,jAppSecret); minfo.env->DeleteLocalRef(jAPP_ID); minfo.env->DeleteLocalRef(jAppSecret); minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun call LoginWX over!"); } else { //NoticeMsg::Instance().Showlogon(false); cocos2d::log("JniFun call LoginWX error!"); }#endif#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS cocos2d::log("IosHelper::sendAuthRequest!"); IosHelper::sendAuthRequest();#endif }
在JniFun里面就用到了cocos封装好的JniHelper类,通过上面的代码才会真正调用到Java里面的微信API方法
其中这里需要注意,微信的方法编写是有约束的,打个比方,如果你的项目包名是com.zhijIEgame.www,那么你就要在src下面先新建一个com.zhijIEgame.www.wxAPI的包,这个命名规则是微信要求的,然后在这个包下面创建一个WXEntryAvtivity的类:
package com.ydqp.yjmj.wxAPI;import java.io.ByteArrayOutputStream;import java.io.file;import java.io.IOException;import java.io.inputStream;import java.net.URL;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.content.res.AssetManager;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.WindowManager;import androID.Widget.button;import androID.Widget.Toast;import com.wx.Native;import com.wx.Util;import com.tencent.mm.sdk.constants.ConstantsAPI;import com.tencent.mm.sdk.modelbase.BaseReq;import com.tencent.mm.sdk.modelbase.BaseResp;import com.tencent.mm.sdk.modelmsg.SendAuth;import com.tencent.mm.sdk.modelmsg.SendMessagetoWX;import com.tencent.mm.sdk.modelmsg.ShowMessageFromWX;import com.tencent.mm.sdk.modelmsg.WXAppExtendobject;import com.tencent.mm.sdk.modelmsg.WXImageObject;import com.tencent.mm.sdk.modelmsg.WXMediaMessage;import com.tencent.mm.sdk.modelmsg.WXTextObject;import com.tencent.mm.sdk.modelmsg.WXWebpageObject;import com.tencent.mm.sdk.openAPI.IWXAPI;import com.tencent.mm.sdk.openAPI.IWXAPIEventHandler;import com.tencent.mm.sdk.openAPI.WXAPIFactory;public class WXEntryActivity extends Activity implements IWXAPIEventHandler{ public static String Tag = "YZH"; private static final int TIMEliNE_SUPPORTED_VERSION = 0x21020001; private static WXEntryActivity sContext = null; public static boolean blogonWX = false; public static boolean isShareWX = false; public static boolean isShareURL = false; private static final int THUMB_SIZE = 150; public static final String APP_ID = "*****************************"; public static final String AppSecret = "******************************"; private IWXAPI API; private static final int Scenesession = 0; private static final int SceneTimeline = 1; public static String ReqWxLogin = "ReqWxLogin"; public static String ReqWxShareimg = "ReqWxShareimg"; public static String ReqWxShareTxt = "ReqWxShareTxt"; public static String ReqWxShareUrl = "ReqWxShareUrl"; public static String ReqWXPay = "ReqWXPay"; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sContext = this; Log.d(Tag,"onCreate"); Log.d(Tag,"onCreate1"); Intent intent = getIntent(); Log.d(Tag,"onCreate2"); API = WXAPIFactory.createWXAPI(this,APP_ID,false); Log.d(Tag,"onCreate3"); API.registerapp(APP_ID); Log.d(Tag,"onCreate4"); API.handleIntent(intent,this); Log.d(Tag,"onCreate5"); if (intent.hasExtra(ReqWxLogin)) { reqLogin(); } else if(intent.hasExtra(ReqWxShareimg)) { isShareWX=true; Log.d(Tag,"ReqWxShareimg"); String imgPath = intent.getStringExtra("imgPath"); int nType = intent.getIntExtra("ShareType",0); reqShareimg(imgPath,nType); } else if(intent.hasExtra(ReqWxShareTxt)) { isShareWX=true; Log.d(Tag,"ReqWxShareTxt"); String ShareText = intent.getStringExtra("ShareText"); int nType = intent.getIntExtra("ShareType",0); reqShareTxt(ShareText,nType); } else if(intent.hasExtra(ReqWxShareUrl)) { isShareWX=true; Log.d(Tag,"ReqWxShareUrl"); String ShareUrl = intent.getStringExtra("ShareUrl"); String ShareTitle = intent.getStringExtra("ShareTitle"); String ShareDesc = intent.getStringExtra("ShareDesc"); int nType = intent.getIntExtra("ShareType",0); reqShareUrl(ShareUrl,ShareTitle,ShareDesc,nType); } Log.d(Tag,"onCreate fnish"); finish(); } @OverrIDe protected voID onNewIntent(Intent intent) { super.onNewIntent(intent); Log.d(Tag,"1"); setIntent(intent); Log.d(Tag,"2"); API.handleIntent(intent,"3"); } public voID reqLogin() { SendAuth.Req req = new SendAuth.Req(); req.scope = "snsAPI_userinfo"; req.state = "123"; API.sendReq(req); Log.d(Tag,"reqLogin"); if(!API.openWXApp()) { Toast.makeText(this,"请先安装微信客户端",Toast.LENGTH_LONG).show(); } } public voID reqShareimg(String imgPath,int nType) { file file = new file(imgPath); if (!file.exists()) { Log.d(Tag,"reqShare file not exists:"+imgPath); return; } Bitmap bmp = BitmapFactory.decodefile(imgPath); WXImageObject imgObj = new WXImageObject(bmp); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,128,72,true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp,true); SendMessagetoWX.Req req = new SendMessagetoWX.Req(); req.transaction = buildTransaction("img"); req.message = msg; if(nType==SceneTimeline ) { req.scene = SendMessagetoWX.Req.WXSceneTimeline; } else if(nType==Scenesession ) { req.scene = SendMessagetoWX.Req.WXScenesession; } API.sendReq(req); Log.d(Tag,"reqShare Ok:"+imgPath); } public voID reqShareTxt(String text,int nType) { WXTextObject textObj = new WXTextObject(); textObj.text = text; WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; // msg.Title = "Will be ignored"; msg.description = text; SendMessagetoWX.Req req = new SendMessagetoWX.Req(); req.transaction = buildTransaction("text"); req.message = msg; if(nType==SceneTimeline ) { req.scene = SendMessagetoWX.Req.WXSceneTimeline; } else if(nType==Scenesession ) { req.scene = SendMessagetoWX.Req.WXScenesession; } API.sendReq(req); Log.d(Tag,"reqShareTxt Ok:"+text); } public native voID Share(); public voID reqShareUrl(String url,String Title,String desc,int nType) { WXWebpageObject textObj = new WXWebpageObject(); textObj.webpageUrl = url; WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.Title = Title; msg.description = desc; //if(nType==SceneTimeline ) { AssetManager asm=getAssets(); String s = "weixin_share_icon.png"; inputStream is = null; try { is = asm.open(s); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); }//asm.open(""); Bitmap bmp=BitmapFactory.decodeStream(is); URL res = this.getClass().getResource("icon.png"); Log.d(Tag,"r:"+res); if (null != bmp) { Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,80,true); bmp.recycle(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); thumbBmp.compress(Bitmap.CompressFormat.PNG,100,baos); msg.thumbData = baos.toByteArray(); } } SendMessagetoWX.Req req = new SendMessagetoWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; if(nType==SceneTimeline ) { isShareURL = false; req.scene = SendMessagetoWX.Req.WXSceneTimeline; } else if(nType==Scenesession ) { isShareURL = false; req.scene = SendMessagetoWX.Req.WXScenesession; } API.sendReq(req); Log.d(Tag,"reqShareUrl Ok:"+url); } public voID reqShareTxtCB(String text,int nType) { // send appdata with no attachment WXAppExtendobject appdata = new WXAppExtendobject("lallalallallal","filePath"); boolean bValue = appdata.checkArgs(); if (!bValue) { Log.d(Tag,"reqShareTxtCB Error:"+text); } WXMediaMessage msg = new WXMediaMessage(); msg.Title ="11"; msg.description = "22"; msg.mediaObject = appdata; SendMessagetoWX.Req req = new SendMessagetoWX.Req(); req.transaction = buildTransaction("appdata"); req.message = msg; if(nType==SceneTimeline ) { req.scene = SendMessagetoWX.Req.WXSceneTimeline; } else if(nType==Scenesession ) { req.scene = SendMessagetoWX.Req.WXScenesession; } API.sendReq(req); Log.d(Tag,"reqShareTxtCB Ok:"+text); } @OverrIDe public voID onReq(BaseReq req) { switch (req.getType()) { case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX: Log.d(Tag,"onReq:COMMAND_GETMESSAGE_FROM_WX"); break; case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX: Log.d(Tag,"onReq:COMMAND_SHOWMESSAGE_FROM_WX"); break; default: break; } Log.d(Tag,"onReq:"+req.getType()); } @OverrIDe public voID onResp(BaseResp resp) { String result = null; Log.d(Tag,"errCode:"+resp.errCode); switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: Log.d(Tag,"OK"); result = " *** 作成功"; if(!isShareWX) { isShareWX=!isShareWX; result =" *** 作成功"; String code = ((SendAuth.Resp) resp).code; String Url =code; Log.d(Tag,"OK1"); Log.d(Tag,Url); Native.WxLoginGetAccesstoken(Url); Log.d(Tag,"OK2"); } Log.d(Tag,"Start Share"); if(isShareURL) { Log.d(Tag,"Shareing"); Share(); result ="分享成功"; } Log.d(Tag,"Over Share"); break; case BaseResp.ErrCode.ERR_USER_CANCEL: Log.d(Tag,"quxiao"); result ="用户取消"; Native.WxLoginGetAccesstoken("ERR_USER_CANCEL"); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: Log.d(Tag,"jujue"); result ="用户拒绝"; Native.WxLoginGetAccesstoken("ERR_AUTH_DENIED"); break; default: Log.d(Tag,"buzhIDao"); result ="未知错误"; Native.WxLoginGetAccesstoken("REE_UnkNow"); break; } Toast.makeText(this,result,Toast.LENGTH_LONG).show(); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } }把你申请得到的APP_ID和AppSecret填上去,就可以了,这里只是做了简单的登录功能,其他部分都差不多,调用不同的函数就可以了。 总结
以上是内存溢出为你收集整理的Cocos项目中接入微信SDK全部内容,希望文章能够帮你解决Cocos项目中接入微信SDK所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)