Note that there are some explanatory texts on larger screens.

plurals
  1. POcustom ColorDialogPreference - saving/retrieving as sharedpreference
    primarykey
    data
    text
    <p>I'm having trouble implementing this <a href="http://www.cmwmobile.com/index.php?option=com_content&amp;view=article&amp;id=34%3ahow-to-create-a-colordialogpreference&amp;catid=10%3ablog&amp;Itemid=43" rel="nofollow">color picker</a> 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 <code>ColorDialogPreference.java's</code> <code>int color</code> to a static variable and calling it from the WallpaperService class directly:</p> <p><code>int mColor = com.my.app.ColorDialogPreference.color;</code></p> <p>But since it's static, the color gets reset once the process gets destroyed.</p> <p>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.</p> <p><strong>PreferencesActivity.java:</strong></p> <pre><code>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); } } </code></pre> <p><strong>WallpaperService.java:</strong></p> <pre><code>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 } } } </code></pre> <p>And my <strong>prefs.xml:</strong></p> <pre><code>`&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;PreferenceScreen android:title="Set Text" android:summary="Some summary" &gt; &lt;EditTextPreference android:key="text_to_display" android:title="Some text" /&gt; &lt;/PreferenceScreen&gt; &lt;PreferenceScreen android:title="Colors" android:summary="Any color you want!" &gt; &lt;com.my.app.ColorPickerPreference android:defaultValue="0xffffffff" android:key="preferenceKeyName" android:title="@string/pref_name" android:summary="@string/pref_summary" /&gt; &lt;/PreferenceScreen&gt; &lt;/PreferenceScreen&gt;` </code></pre> <p>If I display the value of <code>int color</code> on the canvas, all that ever comes up is "-65535". I know that <code>PreferencesActivity</code> works to some degree, because retrieving <code>"text_to_display"</code>, 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 <code>ColorDialogPreference.java</code>, but nowhere else?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload