繁体   English   中英

Android:线程可运行和处理程序

[英]Android: Thread Runnable and Handler

我遇到了一个问题,我正在学习android以及如何使用可运行线程和处理程序来更改UI线程,我已经知道如何使用AsynkTask来做到这一点,我只是在尝试学习如何做到这一点。两种方式的问题都是我的应用程序冻结然后崩溃,我认为解决方案是使用postDelayed,但是我不知道如何应用它,这就是我想出的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //MORE CODEE
    handler();
}

我在onCreate上调用的UI线程中的我的处理程序(主要):

private void handler()
    {
        HandlerMessage = new Handler(){

            @Override
            public void handleMessage(Message msg) {

                Bundle infoBundle = msg.getData();

                int index= infoBundle.getInt(KEY_HANDLER_INDEX);

                switch(index)
                {
                case CHANGE_COLOR:

                    eColors selectedColor = (eColors) infoBundle.getSerializable(KEY_HANDLER_COLOR);
                    myTextViewMain.setBackgroundColor(eColors.enumToColor(selectedColor));
                    myLayoutMain.setBackgroundColor(eColors.enumToColor(selectedColor));

                    break;

                case STARTING_EXECUTION:

                    Toast.makeText(getApplicationContext(), "Handler: Sending SOS...",
                            Toast.LENGTH_SHORT).show();
                    myTextViewMain.setBackgroundColor(Color.WHITE);
                    myLayoutMain.setBackgroundColor(Color.WHITE);

                    break;

                case STOP_EXECUTION:

                    Toast.makeText(getApplicationContext(), "Handler: Cancel SOS...",
                            Toast.LENGTH_SHORT).show();
                    System.out.println("Stop");
                    changeColor(currentColor);

                    break;

                default:
                }

            }

        };
    }

我用来创建新线程的内容:

private void workingWithBottle(taskWork work) {

        if(work == taskWork.STOP){

            sosButton.setEnabled(true);
            StingHandler.cancelExecution();

        }else{
            if (work == taskWork.START)
            {
                sosButton.setEnabled(false);
                StingHandler = new handlerMessageInABottle(HandlerMessage);
                StingHandler.run(); 
            }
        }
}

我的Runnable在单独的文件中:

//handlerMessageInABottle.java
package com.izonfreak.lightadvance;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.izonfreak.lightadvance.MainActivity.eColors;

public class handlerMessageInABottle  implements Runnable {

    boolean stopWorking = false;
    Message myMessage;
    Bundle myBundle;
    eColors workColor;
    Handler myHandler;

    private final String KEY_HANDLER_INDEX = "HANDLER_INDEX";
    private final String KEY_HANDLER_COLOR = "HANDLER_COLOR";
    private static final int CHANGE_COLOR=1, STARTING_EXECUTION =2 , STOP_EXECUTION =3;


    handlerMessageInABottle(Handler handler)
    {
        myHandler = handler;

    }
    @Override
    public void run() {
        String mourseMessage = "...---...";
        int i=0, time =0;

        sendToHandler(STARTING_EXECUTION);

        while (true) {

            System.out.println(mourseMessage);

            if(mourseMessage.charAt(i)=='.')
                time = 1000;
            else if (mourseMessage.charAt(i)=='-')
                time = 2000;
            else
                time = 1000;

            i++;

            //sendVisualMessage(time);

            if (stopWorking)break;

            if(i>=mourseMessage.length())i=0;
        }

        sendToHandler(STOP_EXECUTION);

    }

    private void sendToHandler(int action)
    {
        myMessage = myHandler.obtainMessage();
        myBundle = new Bundle();
        myBundle.putInt(KEY_HANDLER_INDEX, action);
        myBundle.putSerializable(KEY_HANDLER_COLOR, workColor);
        myMessage.setData(myBundle);
        myHandler.sendMessage(myMessage);
    }

    private void sendVisualMessage(int duration)
    {
        workColor = eColors.eBLACK;
        sendToHandler(CHANGE_COLOR);

        //myHandler.postDelayed(this, duration);

        workColor = eColors.eWHITE;
        sendToHandler(CHANGE_COLOR);
    }

    public void cancelExecution()
    {
        stopWorking= true;
    }

}

我知道线程正在工作,因为我在LogCat中看到一些输出(“ ...---...”),当应用程序崩溃时,我在LogCat中得到以下内容:

02-19 21:52:28.715: I/System.out(615): ...---...
02-19 21:52:28.715: I/System.out(615): ...---...
02-19 21:52:28.715: I/System.out(615): ...---...
02-19 21:52:28.715: I/System.out(615): ...---...
02-19 21:52:28.735: W/ActivityManager(58):   Force finishing activity com.izonfreak.lightadvance/.MainActivity
02-19 21:52:28.735: I/ActivityManager(58): Killing com.izonfreak.lightadvance (pid=615): user's request
02-19 21:52:28.735: I/Process(58): Sending signal. PID: 615 SIG: 9
02-19 21:52:28.805: I/WindowManager(58): WIN DEATH: Window{460e7a60 Toast paused=false}
02-19 21:52:28.815: I/WindowManager(58): WIN DEATH: Window{460a8f38 com.izonfreak.lightadvance/com.izonfreak.lightadvance.MainActivity paused=true}
02-19 21:52:28.815: I/ActivityManager(58): Process com.izonfreak.lightadvance (pid 615) has died.
02-19 21:52:28.865: W/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 615 uid 10037

就是这样。.我整天都被困在这部分x_x中。

您实际上不是在创建单独的线程。 您的runnable直接在主线程的上下文中运行。 因此,它处于主线程的一个紧密循环中,由于ANR,Android终止了该应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM