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

145 lines
6.1 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;
// 导入必要的Android类
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
// 定义一个继承自Service的类用于同步任务
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; // 任务完成后将任务对象置为null
sendBroadcast(""); // 发送广播,通知同步完成
stopSelf(); // 停止服务
}
});
sendBroadcast(""); // 发送广播,通知开始同步(这里可能需要发送具体信息)
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)) { // 如果附加数据不为空且包含动作名称
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方法
}
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 = 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取消同步服务
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; // 返回mSyncProgress的值
}
}