8

Is it poosible to program a screensaver for a android (not lock screen)?

I am looking for a tutorial on programming a screen saver. The screen saver will activate as the user does nothing more. If the user touches the screen, the screensaver will disappear. Is this possible?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
goojr
  • 230
  • 3
  • 5

3 Answers3

3

You can receive ACTION_SCREEN_OFF in your App with a BroadcastReceiver.In the onReceive method of this BroadcastReceiver, start an activity as the screensaver.And you can register a listener for touch events and when this event occured,finish your App.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
1

I am giving an excerpt for screen-saver : Comments will explain it.

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); Point res=new Point(); getWindowManager().getDefaultDisplay().getSize(res);

        DotsView myView = new DotsView(this,res.x,res.y);
        setContentView(myView);
    }


class DotsView extends View 
{
        int i = 0;Bitmap bmp;Canvas cnv;Rect bounds;Paint p;Random rnd;int width,height;

        public DotsView(Context context ,int width ,int height) 
    {
            super(context);
            this.width = width;
            this.height = height;
            bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        cnv = new Canvas(bmp);
            bounds = new Rect(0 , 0, width,height);
        p = new Paint();
            rnd = new Random();

        p.setColor(Color.RED);
        p.setStyle(Paint.Style.FILL_AND_STROKE);
        p.setDither(false);
        p.setStrokeWidth(3);
        p.setAntiAlias(true);
        p.setColor(Color.parseColor("#CD5C5C"));
        p.setStrokeWidth(15);
        p.setStrokeJoin(Paint.Join.ROUND);
        p.setStrokeCap(Paint.Cap.ROUND);
     }

     @Override
         protected void onDraw(Canvas c) 
    {//Canvas c gets recycled , so drawing on it will keep erasing previous causing animation...we dont want that.
            p.setColor(Color.argb(255, rnd.nextInt(255),rnd.nextInt(255), rnd.nextInt(255)));
    //cnv is outside object canvas, so drawing on it will append, no recycle, we want that.
            cnv.drawPoint(rnd.nextInt(width), rnd.nextInt(height), p); 
    //drawing on cnv canvas, draws on bitmap bmp
        //c.drawBitmap(bmp,src_rect,dest_rect,paint):-
    // crop as per src_rect(null means full), and scale to dest_rect, draw using paint object-null for 'as it is'.  
        c.drawBitmap(bmp, null, bounds , null);
    //make onDraw() recursive but dont make blocking-loop & not indefinite condition
        if(i<1000) {  i++;invalidate();  } 

To see full code see here:- Code-Example-Screen-Saver-Colorful-Dots

0

Android Screen Saver Sample Code

Try to search first for answers before asking for something

Community
  • 1
  • 1
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
  • 4
    Thanks. I found this answer too, but it's not the same. They talking about lock screen and this is not really helpful for my question. – goojr Dec 05 '11 at 08:53