diff --git a/GTaskSyncService.java b/GTaskSyncService.java new file mode 100644 index 0000000..fe51c6c --- /dev/null +++ b/GTaskSyncService.java @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// 定义了一个名为GTaskSyncService的类,它继承自Service类,用于处理同步任务。 +package net.micode.notes.gtask.remote; + +import android.app.Activity; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; + +public class GTaskSyncService extends Service { + // 定义同步操作的Action字符串名称常量 + public final static String ACTION_STRING_NAME = "sync_action_type"; + + // 定义开始同步的Action常量 + public final static int ACTION_START_SYNC = 0; + + // 定义取消同步的Action常量 + public final static int ACTION_CANCEL_SYNC = 1; + + // 定义无效的Action常量 + public final static int ACTION_INVALID = 2; + + // 定义同步服务广播的名称常量 + public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service"; + + // 定义同步服务广播是否正在同步的额外数据键 + public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing"; + + // 定义同步服务广播进度信息的额外数据键 + public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg"; + + // 定义同步任务的静态引用,用于控制同步操作 + private static GTaskASyncTask mSyncTask = null; + + // 定义同步进度的静态字符串,用于记录同步进度信息 + private static String mSyncProgress = ""; + + // 开始同步的方法 + private void startSync() { + // 如果同步任务尚未启动,则创建一个新的同步任务 + if (mSyncTask == null) { + mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() { + // 同步完成时调用的方法 + public void onComplete() { + // 同步完成后,将同步任务设置为null,并发送广播,然后停止服务 + mSyncTask = null; + sendBroadcast(""); + stopSelf(); + } + }); + sendBroadcast(""); + // 执行同步任务 + mSyncTask.execute(); + } + } + + // 取消同步的方法 + private void cancelSync() { + // 如果同步任务正在运行,则取消同步 + if (mSyncTask != null) { + mSyncTask.cancelSync(); + } + } + + // Service创建时调用的方法 + @Override + public void onCreate() { + // 初始化同步任务为null + mSyncTask = null; + } + + // 处理启动命令的方法 + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // 获取Intent的额外数据 + Bundle bundle = intent.getExtras(); + // 如果额外数据不为空且包含ACTION_STRING_NAME键 + if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) { + // 根据ACTION_STRING_NAME的值执行不同的操作 + switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) { + case ACTION_START_SYNC: + // 开始同步 + startSync(); + break; + case ACTION_CANCEL_SYNC: + // 取消同步 + cancelSync(); + break; + default: + // 默认不执行任何操作 + break; + } + // 返回START_STICKY,表示如果服务被系统杀死,会尝试重新创建服务 + return START_STICKY; + } + // 如果Intent没有额外数据,调用父类的onStartCommand方法 + return super.onStartCommand(intent, flags, startId); + } + + // 处理低内存情况的方法 + @Override + public void onLowMemory() { + // 如果同步任务正在运行,取消同步 + if (mSyncTask != null) { + mSyncTask.cancelSync(); + } + } + + // 处理绑定请求的方法,返回null表示不接受绑定 + public IBinder onBind(Intent intent) { + return null; + } + + // 发送广播的方法,用于通知外界同步进度 + public void sendBroadcast(String msg) { + // 更新同步进度字符串 + mSyncProgress = msg; + // 创建一个Intent,用于发送广播 + Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); + // 添加是否正在同步的额外数据 + intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null); + // 添加同步进度信息的额外数据 + intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg); + // 发送广播 + sendBroadcast(intent); + } + + // 静态方法,用于从Activity启动同步 + public static void startSync(Activity activity) { + // 设置Activity上下文 + GTaskManager.getInstance().setActivityContext(activity); + // 创建一个Intent,用于启动同步服务 + Intent intent = new Intent(activity, GTaskSyncService.class); + // 添加额外数据,指定操作为开始同步 + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); + // 启动服务 + activity.startService(intent); + } + + // 静态方法,用于从Context取消同步 + public static void cancelSync(Context context) { + // 创建一个Intent,用于启动同步服务 + Intent intent = new Intent(context, GTaskSyncService.class); + // 添加额外数据,指定操作为取消同步 + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); + // 启动服务 + context.startService(intent); + } + + // 静态方法,用于判断是否正在同步 + public static boolean isSyncing() { + // 返回同步任务是否为null的相反值 + return mSyncTask != null; + } + + // 静态方法,用于获取同步进度字符串 + public static String getProgressString() { + // 返回同步进度字符串 + return mSyncProgress; + } +} \ No newline at end of file