Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make screen flashing/blinking from background service on Android device?

I plan to do background service which will make screen flashing/blinking until user touches screen.

I do not know methods how to make screen flashing - only what learned that could be done with brightness and control via spawned activity.

Want to do flashing with color change on screen i.e. black and white or screen on/off to make it more visible than wih brightness.

like image 864
Chameleon Avatar asked Jan 21 '13 09:01

Chameleon


2 Answers

I used this for screen blinking, In this code my relativeLayout (HomeLayout) will blinking.

Animation animation = new AlphaAnimation(1, 0); // Change alpha
// from fully
// visible to
// invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at

// the
// end so the layout will
// fade back in
relativeLayout.startAnimation(animation);

Add this code, when you touch the scree or button to clear the animation.

relativeLayout.clearAnimation();
like image 69
Aerrow Avatar answered Oct 01 '22 21:10

Aerrow


From your Service you can interact with the WakeLock:

Acquire the WakeLock with:

PowerManager powerMan = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerMan.newWakeLock(
                     PowerManager.SCREEN_DIM_WAKE_LOCK | 
                     PowerManager.ACQUIRE_CAUSES_WAKEUP, "wakelockTag");

Then to turn the screen on:

wakeLock.acquire();

Then to turn it off again:

wakeLock.release(); 

And you could put this into a Thread with a sleep or use a Timer to create the flash.

For example:

new Thread() {
            public void run() {
                boolean screenOn = false;
                for (int i = 0; i < 5; i++) {
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (screenOn) {
                        wakeLock.acquire();
                    } else {
                        wakeLock.release();
                    }
                }
            }
        }.run();

It wouldn't be black/white, just on/off.

If you wanted to go black/white you would have to also disengage the KeyLock (Look up the Android Keyguard), and then push an Activity that was completely Black, then change the Activity to White on a Timer or in a Thread as before. Much more work.

Remember to get permission in the AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK" />

Additional permissions will be required for unlocking the KeyGuard if you go down that route.

like image 29
biddulph.r Avatar answered Oct 01 '22 21:10

biddulph.r