首页 > 解决方案 > 在android中同时运行两个不同的线程

问题描述

我对android中的线程相当陌生,但我有一个问题。

先决条件: 该项目就像一个优步应用程序。我有一个请求驱动程序的弹出模式。如果在一分钟内没有可用的驱动程序,则模式应关闭并通知用户未找到驱动程序。如果在一分钟内收到响应,模态也应该隐藏但显示不同的视图。

我有 2 个不同的线程需要同时运行。我试图将我需要做的所有功能放在一个线程中,但似乎没有成功。

第一个线程在检查是否找到驱动程序时循环。第二个线程隐藏模式显示,如果没有收到驱动程序,它应该终止第一个线程。

问题 第一个线程与第二个线程同时运行,但不会隐藏模式,也不会在驱动程序可用时显示不同的视图。

任何帮助将不胜感激。

请忽略日志,我试图了解线程内的流逻辑

private void showRequestPopUp(View view){
    int width = LinearLayout.LayoutParams.MATCH_PARENT;
    int height = LinearLayout.LayoutParams.MATCH_PARENT;

    locationLayout.setVisibility(View.GONE);
    requestLayout.setVisibility(View.GONE);
    driverLayout.setVisibility(View.GONE);
    geoLocation.setVisibility(View.GONE);

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View requestRidePopUp = Objects.requireNonNull(layoutInflater).inflate(R.layout.modal_request_ride, null);

    boolean focusable = true;
    final PopupWindow popupWindow = new PopupWindow(requestRidePopUp, width, height, focusable);
    popupWindow.setElevation(8);

    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    requestRidePopUp.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            popupWindow.isShowing();
            return true;
        }
    });

    // Thread Initialization
    Thread loadingThread;
    final Thread exitThread;

    final Handler handler = new Handler();
    exitThread = new Thread() {
        @Override
        public void run() {
            Looper.prepare();

            // Update the progress bar
            handler.post(new Runnable() {
                public void run() {
                    if (!Thread.interrupted()) {
                        Log.e(TAG, "Running Exit Thread");

                        while(STOP_THREAD) {
                            if (DR_NAME != null) {
                                Log.e(TAG, "STOP THREAD: ");
                                showDriverDetails();
                                popupWindow.dismiss();
                            }

                            Thread.interrupted();
                            Log.e(TAG, "Thread Status: " + Thread.interrupted());
                        }
                    }
                }
            });
        }
    };

    // Dismiss Loading Window After 1 Minute
    loadingThread = new Thread() {
        @Override
        public void run() {
            Log.e(TAG, "Running Loading Thread");

            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                Log.e(TAG, "EXCEPTION: " + e.getMessage());
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (!interrupted()) {
                        Log.e(TAG, "Hide Loading Screen");

                        if (DR_NAME == null) {
                            Toast.makeText(Home.this, "We could not find any driver near you. Try again Later", Toast.LENGTH_LONG).show();
                            popupWindow.dismiss();

                            locationLayout.setVisibility(View.GONE);
                            requestLayout.setVisibility(View.VISIBLE);
                            driverLayout.setVisibility(View.GONE);
                            geoLocation.setVisibility(View.VISIBLE);

                            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)
                                    geoLocation.getLayoutParams();
                            layoutParams.setMargins(0, 0, 0, 590);
                            geoLocation.setLayoutParams(layoutParams);

                            // End Exit Thread
                            STOP_THREAD = true;

                            Thread.interrupted();
                            Log.e(TAG, "Interrupted Loading Thread: " + Thread.interrupted());
                        }
                    }
                }
            });
        }
    };

    loadingThread.start();
    exitThread.start();

    // Request Ride
    requestRideNotification();

    Button cancelRide = requestRidePopUp.findViewById(R.id.cancelRequest);
    cancelRide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();

            locationLayout.setVisibility(View.GONE);
            requestLayout.setVisibility(View.VISIBLE);
            driverLayout.setVisibility(View.GONE);
            geoLocation.setVisibility(View.VISIBLE);

            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)
                    geoLocation.getLayoutParams();
            layoutParams.setMargins(0, 0, 0, 590);
            geoLocation.setLayoutParams(layoutParams);
        }
    });
}

标签: androidmultithreading

解决方案


我得到了一位朋友的帮助,我所要做的就是重新组织第一个线程和 walla 中的信息流!

private void showRequestPopUp(View view){
    int width = LinearLayout.LayoutParams.MATCH_PARENT;
    int height = LinearLayout.LayoutParams.MATCH_PARENT;

    locationLayout.setVisibility(View.GONE);
    requestLayout.setVisibility(View.GONE);
    driverLayout.setVisibility(View.GONE);
    geoLocation.setVisibility(View.GONE);

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View requestRidePopUp = Objects.requireNonNull(layoutInflater).inflate(R.layout.modal_request_ride, null);

    boolean focusable = true;
    final PopupWindow popupWindow = new PopupWindow(requestRidePopUp, width, height, focusable);
    popupWindow.setElevation(8);

    popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    requestRidePopUp.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            popupWindow.isShowing();
            return true;
        }
    });

    // Thread Initialization
    Thread loadingThread;
    final Thread responseThread;
    final Handler handler = new Handler();

    responseThread = new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            int count = 0;

            while(!STOP_THREAD) {
                count ++;
            }

            if (DR_NAME != null) {
                handler.post(new Runnable() {
                    public void run() {
                        if (!Thread.interrupted()) {
                            Log.e(TAG, "Running Response Thread");
                            Log.e(TAG, "Response Thread DR_NAME: " + DR_NAME);
                            showDriverDetails();
                            popupWindow.dismiss();

                            Log.e(TAG, "Stop Thread: " + STOP_THREAD);
                            Thread.currentThread().interrupt();
                            Log.e(TAG, "Interrupt Response Thread: " + Thread.interrupted());
                        }
                    }
                });
            }
        }
    };

    // Dismiss Loading Window After 1 Minute
    loadingThread = new Thread() {
        @Override
        public void run() {
            Log.e(TAG, "Running Loading Thread");

            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                Log.e(TAG, "EXCEPTION: " + e.getMessage());
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (!interrupted()) {
                        Log.e(TAG, "Active Loading Thread");

                        if (DR_NAME == null) {
                            Toast.makeText(Home.this, "We could not find any driver near you. Try again Later", Toast.LENGTH_LONG).show();
                            popupWindow.dismiss();

                            locationLayout.setVisibility(View.GONE);
                            requestLayout.setVisibility(View.VISIBLE);
                            driverLayout.setVisibility(View.GONE);
                            geoLocation.setVisibility(View.VISIBLE);

                            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)
                                    geoLocation.getLayoutParams();
                            layoutParams.setMargins(0, 0, 0, 590);
                            geoLocation.setLayoutParams(layoutParams);

                            // End Exit Thread
                            STOP_THREAD = true;
                            Thread.currentThread().interrupt();
                            Log.e(TAG, "Interrupt Loading Thread: " + Thread.interrupted());
                        } else {
                            // End Exit Thread
                            STOP_THREAD = false;
                            Thread.currentThread().interrupt();
                            Log.e(TAG, "Interrupt Loading Thread: " + Thread.interrupted());
                        }
                    }
                }
            });
        }
    };

    loadingThread.start();
    responseThread.start();

    // Request Ride
    requestRideNotification();

    Button cancelRide = requestRidePopUp.findViewById(R.id.cancelRequest);
    cancelRide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();

            locationLayout.setVisibility(View.GONE);
            requestLayout.setVisibility(View.VISIBLE);
            driverLayout.setVisibility(View.GONE);
            geoLocation.setVisibility(View.VISIBLE);

            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams)
                    geoLocation.getLayoutParams();
            layoutParams.setMargins(0, 0, 0, 590);
            geoLocation.setLayoutParams(layoutParams);
        }
    });
}

推荐阅读