Hi There! I want to pass a boolean value retrieved from a Preference Activity and send it to cocos2d with JNI. Is there anyone who can help me? Thanks!
What I have done before is to use SharedPreferences on the java side to save some preference values, then have a C*+ class to call a java method from cocos2d, so I first have this C*+ helper class:
#ifndef CommonHelper_h
#define CommonHelper_h
extern "C" {
extern char* getTokenDevice();
}
#endif
#include "CommonHelper.h"
#include
#include "platform/android/jni/JniHelper.h"
//set package name here
const char* kHelperPakageName = "com/mycompany/appname/CommonHelper";
using namespace cocos2d;
extern "C" {
char* getTokenDevice()
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, kHelperPakageName, "getTokenDevice", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
return (char*)JniHelper::jstring2string(str).c_str();
}
return "";
}
}
Then a java class:
package com.mycompany.appname;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
public class CommonHelper
{
public static String getTokenDevice()
{
SharedPreferences sharedPrefs = Cocos2dxActivity.getContext().getSharedPreferences("com.mycompany.appname", Activity.MODE_PRIVATE);
return sharedPrefs.getString("token", "");
}
}
So somewhere on your java code you can use the SharedPreferences object to set the value.
You probably could have a more generic function to pass a key you could use to the SharedPreferences (on my case I was getting the device unique toked ID from the push notification system).
I’m trying to solve the problem but I have not a great experience with JNI so, if there is someone who has already created a live wallpaper with cocos2d-x and was able to combine the Java Preference with the C + + code by JNI, it would be very helpful! Thanks
In my case I’m implementing an Android Live Wallpaper and I have to send a boolean value from the Engine Class when the Shared Preference are changed by the onSharedPreferenceChanged method calling my own native c*+ method. I’m not a big expert programmer so I don’t now what I have to do in the native method to get the value and use it in my scene.
Here is some pieces of my code…
in the Engine Java class
<pre>
@Override
public void onSharedPreferenceChanged {
boolean bsqIsVisible = mPrefs.getBoolean;
Log.d;
//this is the native method that pass a boolean value to the c*+ code by JNI
setBsqVisibility(bsqIsVisible);
}
#include "JniPreferences.h"
#include
using namespace cocos2d;
extern "C"
{
void Java_com_sandro_lwptest_MyWallpaperService_setBsqVisibility(JNIEnv *env, jobject thiz, jboolean myBool){
//What I have to do?
}
}
So, What I have to do in the JniPreferences.cpp?
Please Help me. Thanks!
extern "C"
{
void Java_com_sandro_lwptest_MyWallpaperService_setBsqVisibility(JNIEnv *env, jobject thiz, jboolean myBool){
bool response = ( bool ) myBool;
// response is now the boolean sent by JNI.
// You can also do this for jint to int.
// However, for jstring to const char *, you need to use JNIEnv::GetStringUTFChars()
}
}
Ok Lance! It’s a logic step to catch the value but my problem is to pass the value to another class(ex. Preferences.cpp) that stores all the preference values. I have tried to create a class but I’m doing something wrong. Honestly I’m very new to c++ and programming in general so, I’m sorry!
Preferences.h
#ifndef PREFERENCES_H_
#define PREFERENCES_H_
#include "cocos2d.h"
using namespace cocos2d;
class Preferences
{
static bool blueSquareVisibility;
public:
static void setBlueSquareVisibility(bool isVisible);
static bool getBlueSquareVisibility();
};
#endif /* PREFERENCES_H_ */
Preferences.cpp
#include "Preferences.h"
#include
using namespace cocos2d;
bool blueSquareVisibility = true;
void Preferences::setBlueSquareVisibility(bool isVisible)
{
blueSquareVisibility = isVisible;
}
bool Preferences::getBlueSquareVisibility()
{
return blueSquareVisibility;
}
and JniPreferences.cpp
#include "JniPreferences.h"
#include "Preferences.h"
#include
using namespace cocos2d;
extern "C"
{
void Java_com_sandro_lwptest_MyWallpaperService_setBsqVisibility(JNIEnv *env, jobject thiz, jboolean myBool) {
bool responce = (bool) myBool;
Preferences::setBlueSquareVisibility(responce);
}
}
The problem is with the blueSquareVisibility field that is undefined! I’m making a stupid mistake…
What is wrong? Is there a different solution?
Anybody?
You can use for that [[EasyNDK]]
It could be a solution but in my case the main problem is to store the given value from java to a c++ class. I’m having problems with blueSquareVisibility variable in the Preferences.cpp class. The error is - undefined reference to ‘Preferences::blueSquareVisibility’.
Hi Guys, I have solved the problem! Simply deleting the blueSquareVisibility varible declaration in the Preferences.h and declaring it only in the Preferences.cpp.
Now all works well!