2
\$\begingroup\$

I'm writing an Android application that needs to pass a single boolean into an Android game that I am also writing. The idea is that the user does something in the App which will affect how the game operates.

This is tricky with LIBGDX since I need to get the bool value into the Java files of the game, but of course, you can't call Android specific things from within LIBGDX's main Java files. I tried using an intent but of course the same problem persists. I can get the boolean into the MainActivity.Java of the android output of the game, but can't pass it along any further since the android output and the main java files don't know about each other. I have seen a few tutorials that explain how to use set up an interface in the LIBGDX java files that can call android things. This seems like wild overkill for what I want to do.

I've been trying to use Android's Shared Preferences with LIBGDX's Gdx.app.getPreferences, but I can't make it work.

Anyhelp would be MUCH appreciated.

I've set up two hello world applications. One is a standard Android app, with a single button that is supposed to write "true" into the shared preferences. The other is a standard LIBGDX hello world that is supposed to do nothing but check that bool when launched and if true display one image to the screen, if false, display a different one.

Here's the relevant bit of the Android code:

import android.preference.PreferenceManager;

 public void onClick(View view) {
    if (view == this.boolButton){

        final String PREF_FILE_NAME = "myBool";
        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_WORLD_WRITEABLE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("myBool", true); 
        editor.commit();        
    }
}

And here's the relevant bit of the code from the LIBGDX main file:

Preferences prefs = Gdx.app.getPreferences("myBool");
    boolean switcher = prefs.getBoolean("myBool");

    if(switcher == true){
        texture = new Texture(Gdx.files.internal("data/worked512.png"));
        prefs.putBoolean("myBool", false);
        }
        else
        {
        texture = new Texture(Gdx.files.internal("data/libgdx.png"));
        }

Everything compiles fine, it just doesn't work. I've spent HOURS googling trying to find a way to pass this single boolean from android into a LIBGDX main and I'm totally stumped.

Thanks for your help.

\$\endgroup\$
1

3 Answers 3

3
\$\begingroup\$

Even if the interface thing sounds like overkill, it will solve your problem, and is not much work. However, here's another solution: Just pass the value to your game when you create it, no need for the interface.

Example:

public class MyLibgdxGame implements ApplicationListener {
    // Or extends Game

    boolean passedInFlag;

    public MyLibgdxGame(boolean flag) {
        this.passedInFlag = flag;
    }

    // rest of the methods
}

Then you can use that boolean in your game.

Then you will need to change your starters to pass in a boolean. For Android:

public class AndroidStarter extends AndroidApplication {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean myBoolToPass = false;

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        // config stuff

        initialize(new MyLibgdxGame(myBoolToPass), cfg);
  }
}

All you do is pass it through a normal constructor.

\$\endgroup\$
1
  • \$\begingroup\$ This is the best solution imo. \$\endgroup\$
    – Krtko
    Commented Jun 6, 2015 at 23:29
0
\$\begingroup\$

Try some read firstly: http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup .

\$\endgroup\$
1
  • \$\begingroup\$ Ya I've seen that. Like I said, HOURS of Googling. This is the interface thing I mentioned that just feels like silly over kill. \$\endgroup\$ Commented Aug 1, 2012 at 0:57
0
\$\begingroup\$

Have you tried using postRunnable?

http://code.google.com/p/libgdx/wiki/ApplicationThreading

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .