阻塞线程的办法&&创建子线程
Dialog progressDialog = new Dialog(MainActivity.this, R.style.toast_dialog);
progressDialog.setContentView(View.inflate(MainActivity.this, R.layout.dialog_loading_base_layout, null), new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
progressDialog.setCancelable(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.getWindow().setDimAmount(0F);
progressDialog.show();
public class TestThread {
private HandlerThread TestThread;
private Handler mHandler;
private static final String NAME = "TestThread";
private volatile static TestThread instance;
public static TestThread getInstance() {
if (instance == null) {
synchronized (TestThread.class) {
if (instance == null) {
instance = new TestThread();
}
}
}
return instance;
}
private TestThread() {
if (Process.THREAD_PRIORITY_URGENT_AUDIO >= Process.THREAD_PRIORITY_URGENT_AUDIO &&
Process.THREAD_PRIORITY_URGENT_AUDIO <= Process.THREAD_PRIORITY_LOWEST) {
this.TestThread = new safetyHandlerThread(NAME, Process.THREAD_PRIORITY_URGENT_AUDIO);
} else {
this.TestThread = new safetyHandlerThread(NAME, Process.THREAD_PRIORITY_URGENT_AUDIO);
}
this.TestThread.start();
this.mHandler = new Handler(TestThread.getLooper());
}
public Handler getHandler() {
return mHandler;
}
public Looper getLooper() {
return mHandler.getLooper();
}
public void post(final Runnable r) {
this.mHandler.post(r);
}
public void postDelay(Runnable r, long timeDelay) {
this.mHandler.postDelayed(r, timeDelay);
}
public static void destroy() {
if (instance == null || instance.TestThread == null) {
return;
}
instance.TestThread.quit();
}
private static class safetyHandlerThread extends HandlerThread {
public safetyHandlerThread(String name, int priority) {
super(name, priority);
}
@Override
public void run() {
try {
super.run();
} catch (Throwable t) {
}
}
private void restoreLoop() {
while (true) {
try {
Looper.loop();
} catch (Throwable t) {
}
}
}
}
}
posted on 2021-07-07 17:21 zhang11111wei 阅读(73) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步