简体   繁体   中英

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 ie black and white or screen on/off to make it more visible than wih brightness.

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM