我想在按下后退按钮和按钮点击时停止我的线程(Android工作室)

时间:2017-12-22 03:40:35

标签: android multithreading

我的Android应用程序有两个活动,当我按下主活动中的单击按钮时,它移动到第二个活动并使用以下线程功能读取蓝牙串行数据值。我想按后退按钮时停止线程。或者按下第二个活动上的点击按钮。请帮助我是android的新手。

我的线程功能

void beginListenForData()
{
    final Handler handler = new Handler();

    stopThread = false;
    buffer = new byte[1024];
    Thread thread  = new Thread(new Runnable()
    {
        public void run()
        {
            while(!Thread.currentThread().isInterrupted() && !stopThread)
            {

                try
                {
                    int byteCount = inputStream1.available();
                    if(byteCount > 0)
                    {
                        byte[] rawBytes = new byte[byteCount];
                        inputStream1.read(rawBytes);
                        final String string=new String(rawBytes,"UTF-8");

                        handler.post(new Runnable() {
                            public void run()
                            {


                                recDataString.append(string);                                      //keep appending to string until ~

                                //keep appending to string until ~
                                int endOfLineIndex = recDataString.indexOf("~");                    // determine the end-of-line
                                if (endOfLineIndex > 0) {                                           // make sure there data before ~
                                    //                   String dataInPrint = string.substring(0, endOfLineIndex);    // extract string


                                    if (recDataString.charAt(0) == '#')                             //if it starts with # we know it is what we are looking for
                                    {

                                        String sensor0 = recDataString.substring(1, 4);             //get sensor value from string between indices 1-5
                                        String sensor1 = recDataString.substring(5,9);            //same again...




                                            stringToFloat1 = Float.parseFloat(sensor0);
                                            stringToFloat2 = Float.parseFloat(sensor1);

                                            floatToInt1 =  (int)stringToFloat1 ;
                                            floatToInt2 =  (int)stringToFloat2;
                                        if ((floatToInt1 != lastFloatToInt1) ||(floatToInt2 != lastFloatToInt2 )){

                                            Log.d("hell=" , "ohhhhhhhhhhhhhhhh");
                                            movieMethod();
                                        }

                                        lastFloatToInt1 = floatToInt1 ;
                                        lastFloatToInt2 = floatToInt2 ;
                                    }
                                    recDataString.delete(0, recDataString.length());                    //clear all string data
                                }
                            }
                        });

                    }
                }
                catch (IOException ex)
                {
                    stopThread = true;
                }
            }
        }
    });
    thread.start();
}

3 个答案:

答案 0 :(得分:0)

据我所知,你知道如何停止线程,但你不知道你把stopThread的标志放在哪里?然后下面是积分。

public class SecondActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Button myButton =   findViewById(R.id.my_button_id_from_xml);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopThread = true;
            }
        });
    }

    //Back button listener
    @Override
    public void onBackPressed() {
        stopThread = true;
        super.onBackPressed();
    }

}

答案 1 :(得分:0)

使用此:

Button button;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
         button =   findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yourThread.interrupt();
            }
        });
    }

    //Back button listener
    @Override
    public void onBackPressed() {
        yourThread.interrupt();
        super.onBackPressed();
    }

}

答案 2 :(得分:0)

您可以使用Activity overriden Method - :

@Override
    public void onBackPressed() {
        super.onBackPressed();
       //Do your stuff here
    }