Question

I'm having trouble implementing this color picker into my live wallpaper. It works just fine out of the box by itself (I can add it to my preferences menu, open up the widget and select a color), however I want to use that selected color to detail an EditText field that the user will see on the homescreen canvas. I can easily achieve this by changing ColorDialogPreference.java's int color to a static variable and calling it from the WallpaperService class directly:

int mColor = com.my.app.ColorDialogPreference.color;

But since it's static, the color gets reset once the process gets destroyed.

I've tried saving the variable via a SharedPreferences Editor inside my PreferencesActivity.java (see below), but can't seem to retrieve that variable in the WallpaperService class.

PreferencesActivity.java:

package com.my.app;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class MyPreferencesActivity extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {

@SuppressWarnings("deprecation")
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SharedPreferences preferences = 
getSharedPreferences("prefs", MODE_WORLD_WRITEABLE);
getPreferenceManager().setSharedPreferencesName("preferenceKeyName");


SharedPreferences.Editor editor = preferences.edit();


editor.putInt("preferenceKeyName", Color.GRAY);
editor.commit();

getPreferenceManager().setSharedPreferencesName("text_to_display");
getPreferenceManager().getSharedPreferences().
registerOnSharedPreferenceChangeListener(this);
addPreferencesFromResource(R.xml.prefs);
}

@SuppressWarnings("deprecation")
protected void onDestroy() {
getPreferenceManager().getSharedPreferences().
unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@SuppressWarnings("deprecation")
protected void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().
registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
getPreferenceManager().getSharedPreferences().
registerOnSharedPreferenceChangeListener(this); 
}
}

WallpaperService.java:

package com.my.app;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

public class MyWallpaperService extends WallpaperService {

public MyWallpaperService() {
super(); 
}



@Override
public Engine onCreateEngine() {
return new MyWallpaperEngine();
}

public class MyWallpaperEngine extends Engine 
implements OnSharedPreferenceChangeListener {

Context ctx = getApplicationContext();
SharedPreferences sharedPreferences2 =
getSharedPreferences("prefs", MODE_WORLD_READABLE);

private int mColor = sharedPreferences2.getInt("preferenceKeyName",
Color.RED);

private boolean mVisible = false;

Paint p = new Paint();

float x1 = 0;
float y1 = 60;

private final Handler mHandler = new Handler();

private final Runnable mUpdateDisplay = new Runnable() {
@Override
public void run() {
draw();
}};



private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {


String text = "1. " + mText;
c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
p.setColor(mColor);

c.drawText(text, x, y, p);
}
}               
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mUpdateDisplay);
if (mVisible) {
mHandler.postDelayed(mUpdateDisplay, 16);
}
}

private final Handler handler = new Handler();

private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}

};


@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
mVisible = visible;
if (visible) {


mText = sharedPreferences.getString("text_to_display",
"default");

draw();
} else {
mHandler.removeCallbacks(mUpdateDisplay);
}
}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
handler.removeCallbacks(drawRunner);
mHandler.removeCallbacks(mUpdateDisplay);
}

@Override
public void onDestroy() {
super.onDestroy();
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
draw();
}

}

@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
// TODO Auto-generated method stub
}
}
}

And my prefs.xml:

`<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceScreen android:title="Set Text" android:summary="Some summary" >

<EditTextPreference android:key="text_to_display" android:title="Some text" />
</PreferenceScreen>

<PreferenceScreen android:title="Colors" android:summary="Any color you want!" >

<com.my.app.ColorPickerPreference
android:defaultValue="0xffffffff"
android:key="preferenceKeyName"
android:title="@string/pref_name"
android:summary="@string/pref_summary" />
</PreferenceScreen> 
</PreferenceScreen>`

If I display the value of int color on the canvas, all that ever comes up is "-65535". I know that PreferencesActivity works to some degree, because retrieving "text_to_display", which is an EditText value, works just fine. Something else that is worth noting: if the process gets killed at all (reboot phone or FC), the color that I selected in the widget stays that color if I go back to the widget after having the process killed, so it seems to be saving inside ColorDialogPreference.java, but nowhere else?

Was it helpful?

Solution

Please consider using this color picker: github.com/chiralcode/Android-Color-Picker. It can be used as a preference on PreferenceScreen. Value will be stored in shared preferences with the key you provided.

You should also consider cleaning up your code. My proposition is to change MyPreferencesActivity into this:

public class MyPreferencesActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getPreferenceManager().setSharedPreferencesName("sharedPreferencesFilename");
        addPreferencesFromResource(R.xml.prefs);

    }
}

This is all the code you need. It looks like that you do not fully understand how shared preferences works.

Your code doesn't work, because you are writing preferences to files such as "preferenceKeyName" and "text_to_display", but you are reading them from "prefs".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top