有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

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

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 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.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

就这样。。我一整天都被困在这个地方


共 (1) 个答案

  1. # 1 楼答案

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