3

I make a game with Java and LibGdx

It's a game of skill with a time limit, and the problem is the player can push home button to pause app, and go to task manager, and can see preview of app to seek what he has to find without counting time

task manager

So, I would like to hide the preview of the app when the player push home button to pause

What I tried to do

I override pause and resume handler

@Override
public void pause() {
    Gdx.app.log("LibGDX", "pause");
    state = State.PAUSE;

    Gdx.graphics.setContinuousRendering(false);
    Gdx.graphics.requestRendering();
}

@Override
public void resume() {
    Gdx.app.log("LibGDX", "resume");
    state = State.RUN;
    Gdx.graphics.setContinuousRendering(true);
}

and main part of my public void render(float) function

    switch (state)
    {
        case RUN:
            // game
            break;
        case PAUSE:
            // a black rectangle on screen
            shapeRenderer.setProjectionMatrix(camera.combined);
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            shapeRenderer.setColor(Color.BLACK);
            shapeRenderer.rect(0, 0, GameScreen.WIDTH, GameScreen.HEIGHT);
            shapeRenderer.end();
            break;
        default:
            break;
    }

log tell me that pause and resume are indeed invoked

but in task manager, I still see the game and not the black rectangle

Update 1

to Ridcully answer :

I tried what you suggested

package com.mygdx.game;

import android.os.Bundle;
import android.view.WindowManager;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        // --
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        // --
        initialize(new MyGame(), config);
    }
}

But same thing, I still see the screenshot

I'm with Android Studio 2.1 My smartphone: Oneplus One and Android 6.0.1

Do you have an idea ? thanks

Monir Hadji
  • 119
  • 3
  • 11

2 Answers2

6

There is a flag for that. You can use this little method, e.g. in onCreate() to prevent Android from creating a 'screenshot' for the 'recent activities' list. You can activate/deactivate as you want, e.g. only activate it when the actual game board is visible etc.

/**
 * Sets/clears FLAG_SECURE which prevents system from creating screenshots for 'recent activities' list.
 * Flag should be set while user is in vault.
 *
 * @param set
 */
protected void setWindowFlagSecure(boolean set) {
    if (set) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    } else {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }

EDIT I just found this SO answer, regarding Libgdx applications: Seems you have to set the flag AFTER invoking the initialize() method.

Community
  • 1
  • 1
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • 1
    Don't put it into the Application class, but in the Activities you don't want to show.. – Ridcully Jul 29 '16 at 19:47
  • I think i've just single Activity, it's my AndroidLauncher `Android applications can have multiple activities. Libgdx games should usually only consist of a single activity. Different screens of the game are implemented within libgdx, not as separate activities. The reason for this is that creating a new Activity also implies creating a new OpenGL context, which is time consuming and also means that all graphical resources have to be reloaded.` from https://github.com/libgdx/libgdx/wiki/Starter-classes-&-configuration#android . Other class in my project are just `Screen` and `Game` class – Monir Hadji Jul 29 '16 at 20:30
  • Ah, I have no experience with Libgdx, but it seems the AndroidApplication class (which your AndroidLauncher extends) is in fact inheriting from Activity (I alread wondered that Application class has access to Window). Maybe the Libgdx surroundings manipulate the window flags too. You can try to put a breakpoint in onPause() method and check if the SECURE flag is still set then -- or just try and set the flag in onPause(). – Ridcully Jul 30 '16 at 04:30
  • Yep, I was right -- please see my EDIT in my answer. – Ridcully Jul 30 '16 at 04:34
  • thanks for your answers, I tried to set the flag after invoking initialize() methode but doesn't work like alan_derua said in this thread http://stackoverflow.com/questions/33941381/flag-secure-not-working-on-libgdxs-androidapplication . I give up the idea of disable screen save, I will resolve my problem differently – Monir Hadji Aug 04 '16 at 21:08
-2

You can do that using

public class SampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);

setContentView(R.layout.main);
}
}

Make sure you do that before you call setContentView.

Kowshick
  • 142
  • 3