Skip to content

Instantly share code, notes, and snippets.

@kinjouj
Last active May 22, 2021 20:01
Show Gist options
  • Save kinjouj/4093016 to your computer and use it in GitHub Desktop.
Save kinjouj/4093016 to your computer and use it in GitHub Desktop.
android.service.dreams.DreamService(DayDream) Example 2013/02/27 update settingsActivity
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="sample.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:allowBackup="false">
<activity android:name=".SampleDreamSettingActivity" />
<service android:name="SampleDreamService">
<intent-filter>
<action android:name="android.service.dreams.DreamService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.service.dream" android:resource="@xml/dream" />
</service>
</application>
</manifest>
<dream
xmlns:android="http://schemas.android.com/apk/res/android"
android:settingsActivity="sample.test/.SampleDreamSettingActivity" />
<?xml version="1.0" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Category">
<EditTextPreference
android:key="sample_test_dream_message"
android:title="message"
android:summary="message"
android:dialogTitle="message" />
</PreferenceCategory>
</PreferenceScreen>
package sample.test;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.service.dreams.DreamService;
public class SampleDreamService extends DreamService {
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
setInteractive(false);
setFullscreen(true);
setContentView(new SampleView(this));
}
}
package sample.test;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class SampleDreamSettingActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
FragmentTransaction tx = getFragmentManager().beginTransaction();
tx.replace(android.R.id.content, new SamplePreferenceFragment());
tx.commit();
}
public static class SamplePreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.prefs);
}
}
}
package sample.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SampleView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "SampleView";
private static final int[] colors = new int[]{
Color.RED,
Color.WHITE,
Color.BLUE,
Color.GREEN,
Color.YELLOW
};
private int width;
private int height;
private SurfaceHolder holder;
public SampleView(Context ctx) {
super(ctx);
holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder2) {
Log.v(TAG, "surfaceCreated");
new Thread() {
@Override
public void run() {
Paint paint = new Paint();
paint.setAntiAlias(true);
Canvas c = null;
while (true) {
c = holder.lockCanvas();
if (c == null) {
break;
}
float x = (float)(Math.random() * width);
float y = (float)(Math.random() * height);
paint.setColor(getColor());
c.drawCircle(x, y, 5f, paint);
holder.unlockCanvasAndPost(c);
}
}
}.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.v(TAG, "surfaceChanged");
this.width = width;
this.height = height;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.v(TAG, "surfaceDestroyed");
}
private int getColor() {
int color = (int)(Math.random() * colors.length);
return colors[color];
}
}
@mikeNG
Copy link

mikeNG commented Jan 17, 2014

Hello,
I've used your template to add settings in android's kitkat DessertCase dream, but I'm having some trouble. When I change a preference and launch the dream, the dream does not load the new preferences and acts like nothing changed. I have to reboot the device in order to load the correct preference.

Do you have any clue what I could be missing? Here's the code http://review.cyanogenmod.org/#/c/55041/

Thanks in advance for any hints,
Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment