Доброго времени суток.
Проблема состоит в том, что написанные мной живые обои с использованием libgdx довольно часто закрываются системой. Как я предполагаю это связано с убийством сервисов для освобождения памяти. Но разве у живых обоев не должно быть хотя бы более высокого приоритета? И как можно предотвратить такое?
Сами обои по максимуму оптимизированы, используют как можно меньше ресурсов и освобождают их при всех возможных паузах
AndroidLauncher класс
public class AndroidLauncher extends AndroidLiveWallpaperService {
@SuppressLint("ApplySharedPref")
@Override
public void onCreateApplication () {
final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useCompass = false;
config.useWakelock = false;
config.useAccelerometer = false;
config.getTouchEventsForLiveWallpaper = true;
config.useRotationVectorSensor = false;
config.useGyroscope = false;
//установка параметров в случае первого запуска
SharedPreferences preferences = getSharedPreferences("settings",MODE_PRIVATE);
if(preferences.getStringSet("charecters",null) == null) {
SharedPreferences.Editor editor = preferences.edit();
Set<String> stringSet = new android.support.v4.util.ArraySet<>();
stringSet.add("tim");
stringSet.add("mil");
editor.putStringSet("charecters", stringSet);
editor.commit();
}
//Костыль для оптимизации
SomeBody.charecters = getSharedPreferences("settings",MODE_PRIVATE).getStringSet("charecters",null);
final ApplicationListener listener = new Wallpaper();
initialize(listener, config);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return AndroidLiveWallpaperService.START_STICKY;
}
}
Данный код тоже не очень помогает
public int onStartCommand(Intent intent, int flags, int startId) {
return AndroidLiveWallpaperService.START_STICKY;
}
Сам Wallpaper довольно простой
public class Wallpaper implements ApplicationListener,AndroidWallpaperListener {
static final float SP = 64; //маштаб
private SpriteBatch batch;
private OrthographicCamera camera;
private Color backgroundColor;
private Texture backgroundTexture;
private World world;
private ArrayList<SomeBody> someBodies; // Лист содержащий информацию для отрисовки объектов
@Override
public void create () {
Preferences preferences = Gdx.app.getPreferences("settings");
batch = new SpriteBatch();
camera = new OrthographicCamera();
world = new World(new Vector2(0,0),false);
world.setContactListener(new WallpaperContactListener());
createWorldBorder();
someBodies = SomeBody.createSomeBodies(world);
backgroundColor = new Color(preferences.getInteger("red",255)/255.0f,preferences.getInteger("green",255)/255.0f,preferences.getInteger("blue",255)/255.0f,1);
if (backgroundTexture != null) backgroundTexture.dispose();
String backString = preferences.getString("background","star1");
if (backString.equals("color"))backgroundTexture = null;
else backgroundTexture = new Texture(Gdx.files.internal("star1.png"));
if(preferences.getBoolean("first",true)){
if (backgroundTexture != null) backgroundTexture.dispose();
backgroundTexture = new Texture(Gdx.files.internal("star1.png"));
}
}
private void createWorldBorder(){
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(0,0);
bodyDef.allowSleep = true;
bodyDef.fixedRotation = true;
bodyDef.bullet = true;
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.restitution = 1;
fixtureDef.friction = 1;
fixtureDef.density = 1;
ChainShape shape = new ChainShape();
shape.createChain(new Vector2[]{
new Vector2(0,Gdx.graphics.getHeight()/(SP*16)),
new Vector2(Gdx.graphics.getWidth()/(SP*16),0),
new Vector2(Gdx.graphics.getWidth()/SP - Gdx.graphics.getWidth()/(SP*16) ,0),
new Vector2(Gdx.graphics.getWidth()/SP,Gdx.graphics.getHeight()/(SP*16)),
new Vector2(Gdx.graphics.getWidth()/SP,Gdx.graphics.getHeight()/SP - Gdx.graphics.getHeight()/(SP*16)),
new Vector2(Gdx.graphics.getWidth()/SP - Gdx.graphics.getWidth()/(SP*16),Gdx.graphics.getHeight()/SP),
new Vector2(Gdx.graphics.getWidth()/(SP*16),Gdx.graphics.getHeight()/SP),
new Vector2(0,Gdx.graphics.getHeight()/SP - Gdx.graphics.getHeight()/(SP*16)),
new Vector2(0,Gdx.graphics.getHeight()/(SP*16))
});
fixtureDef.shape = shape;
Body border = world.createBody(bodyDef);
border.createFixture(fixtureDef);
border.setUserData("border");
shape.dispose();
}
private void drawColorBackground(Color color){
if (color != null){
Gdx.gl.glClearColor(color.r,color.g,color.b,color.a);
}else {
Gdx.gl.glClearColor(0, 0, 0, 1);
}
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
private void drawTextureBackground(SpriteBatch batch,Texture texture){
if (texture == null)return;
batch.draw(texture,0,0,Gdx.graphics.getWidth()/SP,Gdx.graphics.getHeight()/SP);
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width/SP;
camera.viewportHeight = height/SP;
camera.position.set(width/(2*SP),height/(2*SP),0);
camera.update();
}
@Override
public void render () {
drawColorBackground(backgroundColor);
batch.setProjectionMatrix(camera.combined);
batch.begin();
drawTextureBackground(batch,backgroundTexture);
world.step(1/Gdx.graphics.getDeltaTime(),8,3);
for (SomeBody sb : someBodies) {
if (sb.body != null) {
if (sb.body.getPosition().x < 0 || sb.body.getPosition().y < 0 || sb.body.getPosition().x > Gdx.graphics.getWidth()/SP || sb.body.getPosition().y > Gdx.graphics.getHeight()/SP){
world.destroyBody(sb.body);
sb.body = null;
continue;
}
sb.update();
sb.draw(batch);
}
}
batch.end();
}
@Override
public void pause() {
for (SomeBody sb : someBodies) {
sb.dispose();
}
if (batch != null) batch.dispose();
world.dispose();
if (backgroundTexture != null)backgroundTexture.dispose();
}
@Override
public void resume() {
batch = new SpriteBatch();
world = new World(new Vector2(0,0),false);
world.setContactListener(new WallpaperContactListener());
createWorldBorder();
Preferences preferences = Gdx.app.getPreferences("settings");
backgroundColor.set(preferences.getInteger("red",255)/255.0f,preferences.getInteger("green",255)/255.0f,preferences.getInteger("blue",255)/255.0f,1);
someBodies = SomeBody.createSomeBodies(world);
if(preferences.getBoolean("first",true)){
if (backgroundTexture != null) backgroundTexture.dispose();
backgroundTexture = new Texture(Gdx.files.internal("star1.png"));
}
if (backgroundTexture != null) backgroundTexture.dispose();
String backString = preferences.getString("background","star1");
if (backString.equals("color"))backgroundTexture = null;
else backgroundTexture = new Texture(Gdx.files.internal("star1.png"));
}
@Override
public void dispose () {
if (batch != null)batch.dispose();
for (SomeBody sb : someBodies) {
sb.dispose();
}
backgroundTexture.dispose();
}
@Override
public void offsetChange(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
}
@Override
public void previewStateChange(boolean isPreview) {
}
@Override
public void iconDropped(int x, int y) {
}
}
Был бы очень благодарен советам, подсказкам и ответам. Спасибо