首先还是要先去看微信开发者文档
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Share_and_Favorites/Android.html
下载微信的范例项目可以更加快速接入,不然看文档会一脸懵逼
首先安卓端要引入微信sdk,在项目的build.gradle的 dependencies里面引入
api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
dependencies {
implementation fileTree(dir: '../libs', include: ['*.jar','*.aar'])
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
implementation fileTree(dir: "C:/CocosDashboard_1.0.12/resources/.editors/Creator/2.4.4/resources/cocos2d-x/cocos/platform/android/java/libs", include: ['*.jar'])
implementation project(':libcocos2dx')
//implementation(name: 'open_ad_sdk', ext: 'aar')
implementation 'com.android.support:appcompat-v7:28.0.0'
//implementation 'com.android.support:support-v4:24.2.0'
api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
然后构建一下 接入成功后微信的api就能使用了
然后在AppActivity.java文件里面加入两个变量
// APP_ID 替换为你的应用从官方网站申请到的合法appID
private static final String APP_ID = "你的appid";
// IWXAPI 是第三方app和微信通信的openApi接口a
static public IWXAPI api;
在onCreate的方法里面先注册到微信端
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Workaround in
// https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// DO OTHER INITIALIZATION BELOW
SDKWrapper.getInstance().init(this);
//微信注册
regToWx();
}
//注册到微信
private void regToWx() {
// 通过WXAPIFactory工厂,获取IWXAPI的实例
api = WXAPIFactory.createWXAPI(this, APP_ID, true);
// 将应用的appId注册到微信
api.registerApp(APP_ID);
//建议动态监听微信启动广播进行注册到微信
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 将该app注册到微信
//api.registerApp(SyncStateContract.Constants._ID);
api.registerApp(APP_ID);
}
}, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
}
然后就可以调用发送消息了
public static String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
}
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//发送图片给朋友 1:发送给朋友 2:发送到朋友圈
public static void sendPengyouquan1(String path,int type){
// 初始化WXImageObject对象
Bitmap bmp = BitmapFactory.decodeFile(path);
WXImageObject imgObj = new WXImageObject(bmp);
// 初始化WXMediaMessage对象
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
// 设置缩略图
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 128, 72, true);
msg.thumbData = bmpToByteArray(thumbBmp, true); // Util工具类在微信官方的范例代码中
// 构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img"); // transaction字段用于唯一标识一个请求
req.message = msg;
//req.scene = WXSceneTimeline;
if(type == 1){
req.scene = WXSceneSession;
}
else if(type == 2){
req.scene = WXSceneTimeline;
}
// 调用api接口发送数据到微信
api.sendReq(req);
}
前端调用静态方法
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendPengyouquan1", "(Ljava/lang/String;I)V", filePath, type);
filePath为存在本地的图片路径
微信端提供两种方法来调用 一种是本地图片 一种是bmp图片数据
这样就能成功挑起微信并发送图片到朋友圈或者发送给朋友了