Android程序设计:多线程同步数据的三种方法的调用代码

本文只做备忘、留影用。具体类参考之前的三篇文章。类名和方法名稍有改动。

    //三种方法的定义
    private SyncCaseAsyncTask                   caseSyncTask    = null;
    private SyncCaseAsyncTask.onSyncCompleted   syncCompletedEventHandler;

    private SyncCaseThread                      caseSyncThread  = null;
    private SyncCaseThread.onCaseSync2Server    sync2ServerEventHandler;

    private SyncCaseHelper                      mSyncCaseHelper = null;
    private SyncCaseHelper.onSyncCompleted      mSyncCompletedEventHanlder;
    private SyncCaseHelper.onCaseSync2Server    mSync2ServerEventHanlder;

    private static Handler mHandler;

    //三种方法的调用
    // --方法1-AsyncTask
    if (caseSyncTask == null) {
        // caseSyncTask = new SyncCaseAsyncTask();
        // caseSyncTask.addCase(mDatas.get(mPosition));
        // caseSyncTask.setOnSyncCompleted(syncCompletedEventHandler);
        // caseSyncTask.execute();
    }
    // --方法1-模仿AsyncTask的Thread
    if (caseSyncThread == null) {
        // caseSyncThread = new SyncCaseThread();
    } else {
        // caseSyncThread.addCase(mDatas.get(mPosition));
        // caseSyncThread.setOnSyncCase2Server(sync2ServerEventHandler);
        // caseSyncThread.setOnSyncCompleted(new onSyncCompleted() {
        //
        // @Override
        // public void SyncCompleted() {
        // Logs.i("2.全部完成。");
        // }
        // });
        // caseSyncThread.start();
    }
    // --方法3-SyncCaseHelper
    if (mSyncCaseHelper == null) mSyncCaseHelper = new SyncCaseHelper();

    mSyncCaseHelper.addCase(mDatas.get(mPosition));
    mSyncCaseHelper.setOnSyncCase2Server(mSync2ServerEventHanlder);
    mSyncCaseHelper.setOnSyncCompleted(mSyncCompletedEventHanlder);
    mSyncCaseHelper.doJobs();

    //三种方法的事件处理:1
    syncCompletedEventHandler = new SyncCaseAsyncTask.onSyncCompleted() {
        @Override
        public void SyncCompleted() {
            showMsg("1--全部完成同步哦--");
            caseSyncTask.clear();
            caseSyncTask = null;
        }
    };

    //三种方法的事件处理:2
    sync2ServerEventHandler = new SyncCaseThread.onCaseSync2Server() {

        @Override
        public void SyncCase2Server(Case cs) {
            Logs.i("2--cs完成同步哦--");
        }
    };
    //三种方法的事件处理:2
    mSyncCompletedEventHanlder = new SyncCaseHelper.onSyncCompleted() {

        @Override
        public void SyncCompleted() {
            Logs.i("3--全部完成同步哦--");
            // --在外部移除列表內容。
            mSyncCaseHelper.clear();
        }
    };

    mSync2ServerEventHanlder = new SyncCaseHelper.onCaseSync2Server() {

        @Override
        public void SyncCase2Server(Case cs) {
            Logs.i("3.更新完畢:" + cs.getCaseName());
            mHandler.obtainMessage(1, cs.getCaseGUID()).sendToTarget();
        }
    };

    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            // showMsg("what:" + msg.what + "case name:" + msg.obj.toString());
            switch (msg.what) {
            case 1:
                String _CaseGUID = msg.obj.toString();
                //--
                break;
            default:
                break;
            }
            super.handleMessage(msg);
        };
    };
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值