Add sharebutton in cocos2d-x for android?

Hi!

When you are developing for IOS you can wrap the share function from objC to c++ and the show it in a viewcontroller but how can I get the same outcome when targeting android?
I need a function that make a windows appear with the share options from a button created with cocos2d-x?

Thanks so much! <3

Thanks to @i__d__k I can help :smile:

In proj.android\src\org\cocos2dx\cpp :

package org.cocos2dx.cpp;

import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;

public class AppActivity extends Cocos2dxActivity {
    private static AppActivity app = null;
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        app = this;
	}
	public static void callShare() {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Check out Horseshoe\nIt's an amazing game and you can get it for free at https://play.google.com/store/apps/details?id=com.yetanothergamedev.horseshoe\nTry it now!");
		app.startActivity(Intent.createChooser(sharingIntent, "Share Horseshoe using"));
    }
}

Include like this:

#if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#endif

Implement the button like this:

    auto share = ui::Button::create("gfx/share.png");
    share->setZoomScale(0.2f);
    share->setPosition(Vec2(origin.x + visibleSize.width - share->getContentSize().width, origin.y + share->getContentSize().height));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	share->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type) {
        if (type == ui::Widget::TouchEventType::ENDED) {
			if(UserDefault::getInstance()->getBoolForKey("Sound",true))
				CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sfx/Select.wav");
            JniMethodInfo methodInfo;
            if (JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/cpp/AppActivity", "callShare", "()V")) {
                methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
                methodInfo.env->DeleteLocalRef(methodInfo.classID);
            }
        }
    });
#endif
this->addChild(share);
1 Like

Thanks for sharing <3

1 Like