在Android的重复任务 [英] repeat task in Android

查看:177
本文介绍了在Android的重复任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试切换屏幕亮度低和高周期性(1S),我认为这code应该工作:

I try to toggle the screen brightness low and high periodically (1s) and I thought this code should work:

SystemClock.sleep(1000);
params.screenBrightness = 0;
getWindow().setAttributes(params);


SystemClock.sleep(1000);
params.screenBrightness = 1;
getWindow().setAttributes(params);

我尝试了这些codeS,但(如果我延长codeS或最后一个)只完成了第二个(即亮度= 1)。正如我怀疑有关,所以我把一个变量int i = 0,那么我每次睡眠功能后++,它显示了我,我= 2毕竟(通过显示字符串)。我认为Android做的总和,但我的屏幕刚反应过来到最后设置,但没有中间的命令。你有任何想法,为什么它是我怎么可以切换屏幕亮度?

I have tried these codes but it only completes the second one (or last one if i extend the codes) (i.e. brightness=1). As I doubt about that so I put a variable int i = 0, then i++ after each sleep function, it shows me i = 2 after all (by displaying string). I think Android does the sum but my screen just react to the last setting but not the intermediate commands. Do you have any idea why it is that and how can I toggle the screen brightness?

我也尝试使用for循环,但没有运气。

I also try to use "for" loop but no luck.

希望能尽快收到您的意见。

Hope to receive your comments asap.

干杯,

推荐答案

我不知道你为什么要亮和变暗屏幕每隔一秒......但如果​​你想上一个时间延迟运行code考虑使用一个处理程序和可运行:

I'm not sure why you want to brighten and darken your screen every other second... But if you want run code on a time delay consider using a Handler and Runnable:

import android.view.WindowManager.LayoutParams;
public class Example extends Activity {
    private LayoutParams mAttributes;
    private Handler mHandler = new Handler();
    private Window mWindow;

    private Runnable onEverySecond = new Runnable() {
        public void run() {
            if(mAttributes.screenBrightness != LayoutParams.BRIGHTNESS_OVERRIDE_FULL)
                mAttributes.screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
            else
                mAttributes.screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_OFF;

            mWindow.setAttributes(mAttributes);
            mHandler.postDelayed(onEverySecond, 1000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWindow = getWindow();
        mAttributes = mWindow.getAttributes();

        mHandler.post(onEverySecond);
    }
}

这篇关于在Android的重复任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆