Android:以特定时间间隔以编程方式打开和关闭屏幕
问题描述:
我需要创建一个应用程序,让屏幕在一段时间内熄灭(例如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
答
我使用的处理程序解决了这个问题。对于任何未来的参考,这里是代码。
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
}
});
发布您的LogcatError ... !!!! – 2013-02-22 05:37:25
这里的问题http://*.com/a/6869014/605027 – Madhur 2013-02-22 06:04:06
我尝试使用Thread.sleep(2000)而不是等待(2000)在try catch块内。但是现在屏幕并没有显示任何变化。它只是闲置,没有任何事情发生。 – newbee 2013-02-22 06:26:59