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

152 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;
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
// 定义一个服务它重写了onStartCommand方法来处理不同的启动命令
public class GTaskSyncService extends Service {
// 常量定义用于在Intent中传递动作字符串
private static final String ACTION_STRING_NAME = "action_string_name"; // 动作字符串的键
private static final int ACTION_INVALID = -1; // 无效的动作值
private static final int ACTION_START_SYNC = 0; // 开始同步的动作值
private static final int ACTION_CANCEL_SYNC = 1; // 取消同步的动作值
// 同步任务的状态
private static SyncTask mSyncTask; // 假设SyncTask是一个处理同步任务的类
private static String mSyncProgress; // 同步进度的字符串
// 当服务接收到启动命令时调用
@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
@Override
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; // 返回同步进度字符串
}
}