You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiaomi-Notes/GTaskSyncService.java

144 lines
6.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
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;
// 定义一个名为GTaskSyncService的服务类它继承自Service类
public class GTaskSyncService extends Service {
// 定义一个字符串常量用于在Intent中标识同步操作类型
public final static String ACTION_STRING_NAME = "sync_action_type";
// 定义同步操作类型的常量
public final static int ACTION_START_SYNC = 0; // 开始同步
public final static int ACTION_CANCEL_SYNC = 1; // 取消同步
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() {
mSyncTask = null; // 清空同步任务
sendBroadcast(""); // 发送广播,通知同步完成(无具体消息)
stopSelf(); // 停止服务
}
});
sendBroadcast(""); // 发送广播通知开始同步无具体消息可能用于初始化UI
mSyncTask.execute(); // 执行同步任务
}
}
// 定义一个私有方法,用于取消同步
private void cancelSync() {
if (mSyncTask != null) { // 如果当前有正在进行的同步任务
mSyncTask.cancelSync(); // 取消同步任务
}
}
// 当服务被创建时调用此方法
@Override
public void onCreate() {
mSyncTask = null; // 初始化同步任务为null
}
// 当服务接收到启动命令时调用此方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras(); // 获取Intent中的额外数据
if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) { // 如果Intent包含同步操作类型
switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) { // 根据同步操作类型执行相应操作
case ACTION_START_SYNC:
startSync(); // 开始同步
break;
case ACTION_CANCEL_SYNC:
cancelSync(); // 取消同步
break;
default:
break;
}
return START_STICKY; // 如果服务被杀死系统将会重新创建服务并调用onStartCommand()方法但传入的是null的Intent
}
return super.onStartCommand(intent, flags, startId); // 如果Intent不包含同步操作类型则调用父类方法
}
// 当设备内存不足时调用此方法
@Override
public void onLowMemory() {
if (mSyncTask != null) { // 如果当前有正在进行的同步任务
mSyncTask.cancelSync(); // 取消同步任务以释放内存
}
}
// 实现Service的onBind方法由于此服务不需要绑定因此返回null
public IBinder onBind(Intent intent) {
return null;
}
// 定义一个公开方法,用于发送广播通知同步进度
public void sendBroadcast(String msg) {
mSyncProgress = msg; // 更新同步进度消息
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); // 创建广播Intent
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) {
GTaskManager.getInstance().setActivityContext(activity); // 设置GTaskManager的Activity上下文
Intent intent = new Intent(activity, GTaskSyncService.class); // 创建启动服务的Intent
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); // 添加同步操作类型的额外数据
activity.startService(intent); // 启动服务
}
// 定义一个公开静态方法用于从Context如Activity或Application取消同步服务
public static void cancelSync(Context context) {
Intent intent = new Intent(context, GTaskSyncService.class); // 创建取消服务的Intent
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); // 添加同步操作类型的额外数据
context.startService(intent); // 启动服务以取消同步
}
// 定义一个公开静态方法,用于检查是否正在同步
public static boolean isSyncing() {
return mSyncTask != null; // 返回mSyncTask是否为null的布尔值
}
// 定义一个公开静态方法,用于获取同步进度消息
public static String getProgressString() {
return mSyncProgress; // 返回同步进度消息
}
}