package com.hdl.photovoltaic.other;
|
|
import android.content.Context;
|
import android.os.Handler;
|
import android.os.Looper;
|
|
import com.hdl.photovoltaic.enums.ShowErrorMode;
|
|
/**
|
* 线程逻辑
|
*/
|
public class HdlThreadLogic {
|
private static final Handler handler = new Handler(Looper.getMainLooper());
|
|
/**
|
* 切换回主线程
|
*
|
* @param run 回调
|
*/
|
public static void runMainThread(Runnable run) {
|
try {
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
run.run();
|
} else {
|
handler.post(run);
|
}
|
} catch (Exception e) {
|
exception(e, ShowErrorMode.YES);
|
}
|
}
|
|
/**
|
* 切换回主线程
|
*
|
* @param run 回调
|
* @param context 上下文
|
* @param showErrorMode 是否显示错误
|
*/
|
public static void runMainThread(Runnable run, Context context, ShowErrorMode showErrorMode) {
|
try {
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
run.run();
|
} else {
|
handler.post(run);
|
}
|
} catch (Exception e) {
|
exception(e, showErrorMode);
|
}
|
}
|
|
/**
|
* 子线程
|
*
|
* @param run 回调
|
*/
|
public static void runThread(Runnable run) {
|
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
run.run();
|
} catch (Exception e) {
|
exception(e, ShowErrorMode.NO);
|
}
|
}
|
}).start();
|
|
}
|
|
/**
|
* 子线程
|
*
|
* @param run 回调
|
* @param context 上下文
|
* @param showErrorMode 是否显示错误
|
*/
|
public static void runThread(Runnable run, Context context, ShowErrorMode showErrorMode) {
|
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
run.run();
|
} catch (Exception e) {
|
exception(e, showErrorMode);
|
}
|
}
|
}).start();
|
|
}
|
|
|
private static void exception(Exception e, ShowErrorMode showErrorMode) {
|
if (showErrorMode == ShowErrorMode.NO) {
|
return;
|
}
|
|
handler.post(new Runnable() {
|
@Override
|
public void run() {
|
// Dialog alertDialog = new AlertDialog.Builder(HDLApp.getInstance().getApplicationContext()).
|
// setTitle("抱歉程序出现错误了,点击\"确认\"获取更多详细信息.").
|
// setMessage(e.getMessage()).
|
// create();
|
// alertDialog.show();
|
//提示
|
// AlertDialog alertDialog=new AlertDialog(HDLApp.getInstance().getApplicationContext(), androidx.fragment.R.style.TextAppearance_Compat_Notification);
|
// alertDialog.setTitle("抱歉程序出现错误了");
|
// alertDialog.show();
|
//Toast.makeText(HDLApp.getInstance().getApplicationContext(), "抱歉程序出现错误了", Toast.LENGTH_SHORT).show();
|
|
}
|
});
|
}
|
}
|