1

I am trying to start a dream service. Currently, this is my code:

@SuppressLint("NewApi")
public class DreamLockService extends DreamService {

    private static final String TAG = "DreamLockService";
    public Utility utilObj = new Utility();

    //private Button btnExit;

    private Button btnlogin;
    private EditText lgPass;

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        // Exit dream upon user touch
        setInteractive(true);
        // Hide system UI
        setFullscreen(true);
        // Set the dream layout
        setContentView(R.layout.lockservice);
        //setClickListener();
        Toast.makeText(this, "Lock Service Created", Toast.LENGTH_LONG).show();
    }

    //Use this for initial setup, such as calling setContentView().
    @Override
    public void onDreamingStarted() {
        super.onDreamingStarted();
        // Exit dream upon user touch
        setInteractive(true);
        // Hide system UI
        setFullscreen(true);
        // Set the dream layout
        setContentView(R.layout.lockservice);
        Toast.makeText(this, "Lock Service Created", Toast.LENGTH_LONG).show();
    }

    //Your dream has started, so you should begin animations or other behaviors here.
    public void onDreamingStopped()
    {
        super.onDreamingStopped();
    }

    //Use this to stop the things you started in onDreamingStarted().
    public void onDetachedFromWindow()
    {
        super.onDetachedFromWindow();
    }
}

I was unable to start the dream service from another activity. This is what I used:

Intent tempLock = new Intent(MainActivity.this, DreamLockService.class);
//DreamLockService test = new DreamLockService();
startService(tempLock);

I don't understand why it didn't work. How can a dream service be started from another activity?

afro-Nija
  • 185
  • 1
  • 4
  • 17

3 Answers3

2

To start a Dream service from our own app, please try this.

    IBinder binder = ServiceManager.getService("dreams");

    Parcel data = Parcel.obtain();
    data.writeInterfaceToken("android.service.dreams.IDreamManager");
    Parcel reply = Parcel.obtain();
    try {
        if (binder != null)
            binder.transact(1, data,reply, Binder.FLAG_ONEWAY);
        else
            Log.e("TAG", "dreams service is not running");
    } catch (RemoteException e) {
        e.printStackTrace();
    } 

To use this, your app should be system app and should have dream permissions in the Manifest file and enable dream setting in Settings.

I tried this and it is working.

noam aghai
  • 1,364
  • 3
  • 18
  • 30
Chari D
  • 91
  • 1
  • 4
0

You can start the currently selected screen saver using this code:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.systemui", "com.android.systemui.Somnambulator");
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

Just need to make sure that your dream service is set and enabled.

grebulon
  • 7,697
  • 5
  • 42
  • 66
-3

Did you include it in your manifest and have an <intent-filter> that matches your action?

If it's ok, then try with:

startService(new Intent(this, DreamLockService.class));

An excellent Services tutorial: http://www.vogella.com/articles/AndroidServices/article.html#scheduleservice_startauto

UPDATE:

As it seems you are not sure if your service is running, you can use this solution found here:

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (MyService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
Community
  • 1
  • 1
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
  • This is my manifest file content with the intent filter ` ` But it did not still work – afro-Nija May 14 '13 at 16:50
  • Check LogCat and it should turn up some warnings that may help. Without having all your code and your LogCat is complicated. – Alejandro Colorado May 14 '13 at 21:47
  • 1
    Are you sure it's not running? Or it's the Toast that doesn't show? When you write: 'Toast.makeText(this, "Lock Service Created", Toast.LENGTH_LONG).show();' I think the context is wrong, as "this" is the Service. Try: [link]http://www.jjoe64.com/2011/09/show-toast-notification-from-service.html – Alejandro Colorado May 14 '13 at 22:33
  • I already tried that and it is not working either it did not start even though it is not showing any error. I just dont know why it is not starting or not. – afro-Nija May 16 '13 at 17:30
  • Post your LogCat, so we could better check it. – Alejandro Colorado May 16 '13 at 17:44
  • I found out out from an android [resource](http://android.stackexchange.com/questions/41859/why-doesnt-my-android-start-daydreaming) that it seems it is a service to be started from android calls not from app. which is like a screen saver you cannot start your self, it is very funny though anyway thank you alejandro. – afro-Nija Aug 10 '13 at 06:52