2013-02-22 112 views
0

我需要創建一個應用程序,讓屏幕在一段時間內熄滅(例如10秒),然後繼續,顯示消息2秒鐘並再次打開關閉。這一直不斷重複,直到點擊一個按鈕。 (即,我想在單擊按鈕時停止此活動)。我試圖通過改變屏幕亮度來實現這一點。Android:以特定時間間隔以編程方式打開和關閉屏幕

我的代碼是有點像這樣:

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; 

如果我沒看錯,這應該能正常運行。但是當我運行它時,它會變暗,然後顯示發生錯誤,強制關閉消息。

任何幫助將不勝感激。

這裏是logcat的錯誤:

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 
+0

發佈您的LogcatError ... !!!! – 2013-02-22 05:37:25

+0

這裏的問題http://stackoverflow.com/a/6869014/605027 – Madhur 2013-02-22 06:04:06

+0

我嘗試使用Thread.sleep(2000)而不是等待(2000)在try catch塊內。但是現在屏幕並沒有顯示任何變化。它只是閒置,沒有任何事情發生。 – newbee 2013-02-22 06:26:59

回答

1

我使用的處理程序解決了這個問題。對於任何未來的參考,這裏是代碼。

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 
         } 
        }); 
相關問題