cocos2d手游之微信分享sdk的安卓源代码

前端之家收集整理的这篇文章主要介绍了cocos2d手游之微信分享sdk的安卓源代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这是微信分享的一个重要文件,必须放在“工程全名.wxapi”的目录下面。例如工程名为org.cocos2dx.lua,则WXEntryActivity.java就必须建立在org.cocos2dx.lua.wxapi的目录。

1.WXEntryActivity.java


package com.mz.maoxian.lenovo.wxapi;



import java.io.IOException;
import java.io.InputStream;

import org.cocos2dx.lua.AppActivity;

import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.tencent.mm.sdk.openapi.WXWebpageObject;



import android.app.Activity;
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.widget.Toast;



public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
	public static final String APP_ID = "wxc8a10e8a59add48b";
	// IWXAPI 是第三方app和微信通信的openapi接口
    private static IWXAPI api;
    
    public static void myLog(String str)
    {
    	Log.i("wc",str);
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myLog("onCreate in Weixin entry");
        this.initWX();
    }
    private static String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
    public void initWX(){
    	 // 通过WXAPIFactory工厂,获取IWXAPI的实例
    	api = WXAPIFactory.createWXAPI(this,APP_ID,false);
    	api.registerApp(APP_ID);
        api.handleIntent(getIntent(),this);
    }
//    private static Bitmap getImageFromAssetsFile(AppActivity activity,String fileName)  
//	  {  
//	      Bitmap image = null;  
//	      AssetManager am = activity.getResources().getAssets();  
//	      try  
//	      {  
//	          InputStream is = am.open(fileName);  
//	          image = BitmapFactory.decodeStream(is);  
//	          is.close();  
//	      }  
//	      catch (IOException e)  
//	      {  
//	    	  Log.i("wc","getImageFromAssetsFile error");
//	          e.printStackTrace();  
//	      }  
//	  
//	      return image;  
//	  
//	  }  
    public static void sendImageWithAPI(IWXAPI api,AppActivity activity,int mode,String smallPng,String bigPng){
    	Log.i("wc","smallPng:" + smallPng + " bigPng:" + bigPng);
    	Bitmap bmpSmall = BitmapFactory.decodeFile(smallPng);
    	Bitmap bmpBig = BitmapFactory.decodeFile(bigPng);
    	if(bmpSmall == null){
    		myLog("bmp is null");
    		Toast.makeText(activity,"image path not find,path:" + smallPng,Toast.LENGTH_LONG).show();
    		return;
    	}
    	if(bmpBig == null){
    		myLog("bmp is null");
    		Toast.makeText(activity,path:" + bigPng,Toast.LENGTH_LONG).show();
    		return;
    	}
    	
    	WXImageObject imgObj = new WXImageObject(bmpBig);
		
		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;
		
		msg.thumbData = Util.bmpToByteArray(bmpSmall,true);  // 设置缩略图

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("img");
		req.message = msg;
		
		//聊天
		if(mode == 0){
			req.scene = SendMessageToWX.Req.WXSceneSession;
		}
		//朋友圈
		else if((mode == 1) && (checkCanSendToFriend(api)))
		{
			req.scene = SendMessageToWX.Req.WXSceneTimeline;
		}
		else{
			req.scene = SendMessageToWX.Req.WXSceneSession;
		}

		api.sendReq(req);
		myLog("send img ok");
    }
    public static void sendURLWithAPI(IWXAPI api,String png,String title,String 
    		desc,String url,int mode)
    {	
    	Bitmap bmp = BitmapFactory.decodeFile(png);
    	
    	if(bmp == null){
    		Toast.makeText(activity,path:" + png,Toast.LENGTH_LONG).show();
    		return;
    	}
    	
    	WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = url;
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = title;
		msg.description =  desc;
		Bitmap thumb = bmp;
		msg.thumbData = Util.bmpToByteArray(thumb,true);
		
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;

		//聊天
		if(mode == 0){
			req.scene = SendMessageToWX.Req.WXSceneSession;
		}
		//朋友圈
		else if((mode == 1) && (checkCanSendToFriend(api)))
		{
			req.scene = SendMessageToWX.Req.WXSceneTimeline;
		}
		else{
			req.scene = SendMessageToWX.Req.WXSceneSession;
		}
				
		api.sendReq(req);
    }
    
    
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Log.i("wc","onNewIntent");
//		setIntent(intent);
//        api.handleIntent(intent,this);
	}

	// 微信发送请求到第三方应用时,会回调到该方法
	@Override
	public void onReq(BaseReq req) {
		
		Log.i("wc","onReq");
		
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			//goToGetMsg();		
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			//goToShowMsg((ShowMessageFromWX.Req) req);
			break;
		default:
			break;
		}
	}

	// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
	@Override
	public void onResp(BaseResp resp) {
		
		Log.i("wc","onResp");
 		Log.i("wc",String.format("%d",resp.errCode));

		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:

			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:

			break;
		default:
			
			break;
		}
		
		if(resp.errCode == BaseResp.ErrCode.ERR_OK)
		{
			backToAppActivityWithSuccess(true);
		}
		else
		{
			backToAppActivityWithSuccess(false);
		}
		
		
	}
	private void backToAppActivityWithSuccess(boolean success){
		
		if(success){
			AppActivity.nativeSendMessage("addevent;104");
		}else{
			AppActivity.nativeSendMessage("addevent;105");
		}
		
		Intent intent = new Intent();
		intent.setClass(WXEntryActivity.this,AppActivity.class);
		startActivity(intent);
		finish();
		((WXEntryActivity) WXEntryActivity.this).overridePendingTransition(0,0);
	}
	
	
	private static boolean checkCanSendToFriend(IWXAPI api){
		int wxSdkVersion = api.getWXAppSupportAPI();
		if (wxSdkVersion >= TIMELINE_SUPPORTED_VERSION) {
			return true;
		} else {
			return false;
		}
	}
	
	
}

2. 用法代码
private static IWXAPI api = null;
private void sendWeixinImage(int mode,String bigPng){
		if(api == null)
		{
			api = WXAPIFactory.createWXAPI(this,WXEntryActivity.APP_ID);
			api.registerApp(WXEntryActivity.APP_ID);
		}
		WXEntryActivity.sendImageWithAPI(api,this,mode,smallPng,bigPng);
	}
	private void sendWeixinURL(String png,String desc,int mode){
		if(api == null)
		{
			api = WXAPIFactory.createWXAPI(this,WXEntryActivity.APP_ID);
			api.registerApp(WXEntryActivity.APP_ID);
		}
		WXEntryActivity.sendURLWithAPI(api,png,title,desc,url,mode);
	}
原文链接:https://www.f2er.com/cocos2dx/340331.html

猜你在找的Cocos2d-x相关文章