I need to create an app in which the screen goes off for some time (say 10 seconds) and then goes on, displays a message for 2 seconds and is turned again off. And this keeps on repeating until a button is clicked. (ie. I want to stop this activity when the button is clicked). I have tried implementing this by changing the screen brightness.

My code is somewhat like this:

float dim = 0;
float bright = 1.0f;
WindowManager.LayoutParams lp=getWindow().getAttributes();
bright = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
wait(2000);
lp.screenBrightness = dim;
wait(10000);
lp.screenBrightness = bright;

If I'm not wrong, this should work fine. But when I run it, it goes dim and then shows an error occurred, force-close message.

Any help will be appreciated.

here is the logcat error:

02-22 11:12:50.804: D/AndroidRuntime(10871): Shutting down VM
02-22 11:12:50.804: W/dalvikvm(10871): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-22 11:12:50.859: E/AndroidRuntime(10871): FATAL EXCEPTION: main
02-22 11:12:50.859: E/AndroidRuntime(10871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wakelock/com.example.wakelock.WakeLock}:java.lang.IllegalMonitorStateException: object not locked by thread before wait()
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.os.Looper.loop(Looper.java:130)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at java.lang.reflect.Method.invokeNative(Native Method)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at java.lang.reflect.Method.invoke(Method.java:507)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at dalvik.system.NativeStart.main(Native Method)
02-22 11:12:50.859: E/AndroidRuntime(10871): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
02-22 11:12:50.859: E/AndroidRuntime(10871):    at java.lang.Object.wait(Native Method)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at java.lang.Object.wait(Object.java:395)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at com.example.wakelock.WakeLock.onCreate(WakeLock.java:42)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-22 11:12:50.859: E/AndroidRuntime(10871):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-22 11:12:50.859: E/AndroidRuntime(10871):    ... 11 more
有帮助吗?

解决方案

i solved the problem using handler. for any future references, here is the code.

 float dim = 0.004f; // if you set this 0, it wont work. 0.004 is the least value
 float bright = 1.0f;
 Handler mHandler = new Handler();

    mHandler.post(new Runnable() {

                        @Override
                        public void run() {

                            lp.screenBrightness= bright;
                            getWindow().setAttributes(lp);                              
                            final Handler handler = new Handler(); 
                            Timer t = new Timer();                              
                            t.schedule(new TimerTask() { 
                                    public void run() { 
                                            handler.post(new Runnable() { 
                                                    public void run() { 
                                                    lp.screenBrightness=dim;
                                                    getWindow().setAttributes(lp);
                                                    } 
                                            }); 
                                    } 
                            }, 2000);   //  flashing of screen
                        }
                    });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top