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.
TEST1231/GTaskSyncService.java

269 lines
6.2 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.
*/
// 定义一个名为GTaskSyncService的服务类继承自Service用于同步Google Tasks
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 {
// 定义一个常量字符串,用于标识同步操作的类型
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(""); // 发送广播,可能用于通知开始同步
mSyncTask.execute(); // 执行异步任务
}
}
// 取消同步操作的方法
private void cancelSync() {
if (mSyncTask != null) { // 如果当前有正在执行的同步任务
mSyncTask.cancelSync(); // 取消同步任务
}
}
// 当服务被创建时调用
@Override
public void onCreate() {
mSyncTask = null; // 清空同步任务变量
}
// 当服务被启动时调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras(); // 获取启动服务的Intent中的附加数据
if (bundle != null && bundle.containsKey(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;
}
return START_STICKY; // 如果服务被杀死系统会在内存可用时重启服务并传递最后一个Intent
}
return super.onStartCommand(intent, flags, startId); // 如果没有指定操作类型,则调用父类方法
}
// 当系统内存低时调用
@Override
public void onLowMemory() {
if (mSyncTask != null) { // 如果当前有正在执行的同步任务
mSyncTask.cancelSync(); // 取消同步任务以释放内存
}
}
// 绑定服务时调用,此处不提供服务绑定支持
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); // 设置活动上下文
Intent intent = new Intent(activity, GTaskSyncService.class); // 创建一个新的Intent
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); // 添加操作类型的键值对
activity.startService(intent); // 启动服务
}
// 从Context取消同步服务的方法
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; // 返回是否有正在执行的同步任务
}
// 获取同步进度消息的方法
public static String getProgressString() {
return mSyncProgress; // 返回同步进度消息
}
}