android - 在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);
}
});
}
解决方案
我得到了一位朋友的帮助,我所要做的就是重新组织第一个线程和 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);
}
});
}
推荐阅读
- scala - UnsupportedOperationException:找不到 org.apache.spark.sql.Row 的编码器
- .net - .NET 存储库模式和 Http 请求
- java - 如何使用本机 JAVA 打印 PDF 文件(或打开并打印)
- python - 如何从构成矩形的列表中确定最好的 4 个点?
- node.js - 如何根据 mongoDB 中的搜索 id 显示数组值?
- kotlin - 请问Flow的collect会阻塞执行吗?
- javascript - 如何将前面的“0”添加到小于 10 的天数和月数
- reactjs - 如何在反应中将函数转换为基于类的组件?
- ios - macCatalyst 共享扩展偶尔会阻止输入?
- php - 如何将函数值(字节)转换为十进制值(每个 0-255)