首页 > 代码库 > 新建 开辟线程的类,更加简洁方便

新建 开辟线程的类,更加简洁方便

后台开启线程的函数写法:

更加简洁明了:

public class BackTaskRunner {
    private static final String TAG = "BackTaskRunner";
    private HandlerThread handlerThread;
    private Handler handler;

    private BackTaskRunner() {
        handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());
    }

    public static Handler getHandler() {
        return SingletonHolder.sRunner.handler;
    }

    private static class SingletonHolder {
        static BackTaskRunner sRunner = new BackTaskRunner();
    }
}

调用方法:

 BackTaskRunner.getHandler().post(new Runnable() {
            @Override
            public void run() {
                Logging.d(TAG, "pay() start UNPay");
                //最后一个参数: "01" 代表测试环境    "00" 代表正式环境
                UPPayAssistEx.startPay(activity,null,null,tn,payMode);
            }
        });

 

新建 开辟线程的类,更加简洁方便