根据莫尔斯code-Android的闪烁闪光。如何避免ANR的因睡着了吗? (火炬APP) [英] Blinking Flash according to Morse Code- Android. How to avoid ANRs due to sleeping ? (Torch App)

查看:273
本文介绍了根据莫尔斯code-Android的闪烁闪光。如何避免ANR的因睡着了吗? (火炬APP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我做了一个火把应用......正常的火炬功能完全正常工作,没有任何问题。

So, I made a torch app... The normal Torch function is fully working, no problems.

下面介绍以下功能,可能会导致可能的ANR数
再说了,我想通过闪烁的闪光灯发出SOS信息(莫尔斯code)。
(其111-000-111)
所以这是开 - 开 - 开 - 关 - 关 - 和重复。

The following explains the below function that can cause possible ANRs Say, I want to send out an SOS message (morse code) through the blinking flash. (its 111-000-111) So it's On-On-On-Off-Off-Off- and repeat.

(慢慢读)
我开,一小会儿,然后微微一晃,开了 - 这是区分两个连续开S ......这就是我得到三个不同的闪烁。

(Read Slowly) I "On" it for a little while, then a little flash, "On" again - This is to distinguish two consecutive "On"s ... That's how I get three distinct flashes.

麻烦的是,对于延时,我把线程睡觉。(这种方法的工作最适合我的)

Trouble is, for Time delay, I put the thread to sleep.(This method has worked the best for me)

下面是我的示例函数,它闪烁出SOS:
(我有一个SOS按钮,单独)

Here's my sample function , it flashes out SOS: (I have a seperate button for SOS)

public void sos(View v) {

        String myString = "111000111";

        for (int x = 0; x < myString.length(); x++) {
            if (myString.charAt(x) == '1') {

                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                c.setParameters(p);
                c.startPreview();
                flag = true;

            } else {

                p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                c.setParameters(p);
                flag = false;
            }

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            c.setParameters(p);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        c.setParameters(p);
    }

}

上面闪烁出SOS一次。
不过,虽然这闪烁,闪烁是怎么回事,我的应用程序被冻结了。闪烁SOS是完美的。但它并没有任何按钮点击,甚至后退按钮响应中途停止。

The above flashes out SOS just once. But while this flashing and blinking is going on , my app is frozen up. The Blinking SOS is perfect. But it doesn't respond to any button clicks or even Back Button to stop it midway.

我想这是因为线程(UI)正在睡觉,因此它并不关心什么用户在做)......但肯定点击注册,并且如果按照到Android如果不照顾在时间的规定期限(我想了几秒钟)...该应用程序进入ANR。

I reckon this is because the thread(UI) is sleeping hence it doesn't care about what the user is doing)... But the click surely is registered, and if according to android if it isn't looked after in the stipulated period of time (I think a few seconds) ... The app goes ANR.

所以,我需要与我规定的时间间隔短暂的停顿(正如上面我用300毫秒)。但我也想知道如何让应用同时响应说按钮点击或后退按钮presses,以避免ANR的,给用户关闭在任何时候闪烁的能力。

So, I need brief pauses with intervals defined by me (As above I use 300 milliseconds). But I also want to know how to get the app to simultaneously respond to say Button Clicks, or Back Button Presses so as to avoid ANR's and give the user the ability to shut down the blinking at any time.

如何做到这一点?

修改
Waza_Be的解决方案:

EDIT Waza_Be's Solution:

好吧,这里是根据我的Waza_Be修改功能。这个问题似乎是,它等待300毫秒(对我来说)。但是我有几个postDelay功能 - 因此,我认为它实际上是排队的所有功能于一体,与300毫秒的等待期是有些重叠。这一时期结束后,所有的code一起运行一次,使得它毫无意义。

Okay so here's my modified function according to Waza_Be. The problem seems to be, it does wait 300 milliseconds(in my case) ...But I have several postDelay functions - So I think it's actually lining up all the functions together, and the waiting period of the 300 milliseconds is somewhat overlapping. After this period ends, all the code runs together at once, making it quite pointless.

当我尝试这样做,闪光灯闪烁相当迅速,不与微妙的停顿和闪烁效果我想要的。

When I tried this, the Flash blinks rather swiftly, not with the subtle pauses and blinking effect I want.

public void sos(View v) {
        String myString = "111000111";
        Handler handler = new Handler();

        for (int x = 0; x < myString.length(); x++) {
            if (myString.charAt(x) == '1') {
                     handler.postDelayed(new Runnable() {
                    public void run() {
                        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        c.setParameters(p);
                        c.startPreview();
                    }
                }, 300);

            } else {

                handler.postDelayed(new Runnable() {
                    public void run() {
                        p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        c.setParameters(p);
                    }
                }, 300);

            }


            handler.postDelayed(new Runnable() {
                public void run() {
                    p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    c.setParameters(p);
                }
            }, 300);
        }

        p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        c.setParameters(p);
}
}


我需要单独暂停。或每隔TURN_ON或TURN_OFF为Flash之前说。
在每个循环中,它应该独立等待中的处理程序运行每件code的()。


I need separate pauses. Say after or before every TURN_ON or TURN_OFF for the Flash. In each loop, it should wait individually for each piece of code in the Handler Run().

推荐答案

我解决了这个采用螺纹有点不同。
通过点击一个按钮生成莫尔斯code。

I solved this a little different using Thread. The morse code is generated by clicking a button.

    OnClickListener goMorse = new OnClickListener() {

    @Override
    public void onClick(View v) {

        new Thread () {
            public void run() {
                if(myMorseString != null){
                for (int x = 0; x < myMorseString.length(); x++) {
                    if (myMorseString.charAt(x) == '2') {
                        cam = Camera.open();
                        sleepTime = 500;
                        Parameters p = cam.getParameters();
                        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(p);
                        cam.startPreview();
                        try {
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        // power off after signal
                        cam.stopPreview();
                        cam.release();
                        cam = null;
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    if (myMorseString.charAt(x) == '1') {
                        cam = Camera.open();
                        sleepTime = 250;
                        Parameters p = cam.getParameters();
                        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(p);
                        cam.startPreview();
                        try {
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // power off after signal
                        cam.stopPreview();
                        cam.release();
                        cam = null;
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    if (myMorseString.charAt(x) == '0') {
                        cam = Camera.open();
                        sleepTime = 250;
                        Parameters p = cam.getParameters();
                        cam.setParameters(p);
                        //cam.startPreview();
                        cam.stopPreview();
                        cam.release();
                        cam = null;

                        try {
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                }           
            }}}
        }.start();
    }

};

这篇关于根据莫尔斯code-Android的闪烁闪光。如何避免ANR的因睡着了吗? (火炬APP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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